반응형
jQuery로 iframe의 콘텐츠에 액세스하는 방법은 무엇입니까?
jQuery로 iframe의 컨텐츠에 액세스하려면 어떻게해야합니까? 이 작업을 시도했지만 작동하지 않습니다.
iframe 컨텐츠 : <div id="myContent"></div>
jQuery : $("#myiframe").find("#myContent")
어떻게 액세스 할 수 myContent
있습니까?
jquery / javascript 와 유사합니다 : iframe의 내용에 액세스 하지만 허용 된 답변은 내가 찾고있는 것이 아닙니다.
이 contents()
방법 을 사용해야합니다 .
$("#myiframe").contents().find("#myContent")
출처 : http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/
API 문서 : https://api.jquery.com/contents/
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">
$(function() {
//here you have the control over the body of the iframe document
var iBody = $("#iView").contents().find("body");
//here you have the control over any element (#myContent)
var myContent = iBody.find("#myContent");
});
</script>
</head>
<body>
<iframe src="mifile.html" id="iView" style="width:200px;height:70px;border:dotted 1px red" frameborder="0"></iframe>
</body>
</html>
iframe의 소스가 외부 도메인 인 경우 브라우저는 iframe 컨텐츠를 숨 깁니다 (동일한 원본 정책). 해결 방법은 외부 컨텐츠를 파일 (예 : PHP)로 파일에 저장하는 것입니다.
<?php
$contents = file_get_contents($external_url);
$res = file_put_contents($filename, $contents);
?>
그런 다음 새 파일 내용 (문자열)을 가져 와서 html로 구문 분석하십시오 (예 : jquery).
$.get(file_url, function(string){
var html = $.parseHTML(string);
var contents = $(html).contents();
},'html');
참고 URL : https://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery
반응형
'developer tip' 카테고리의 다른 글
Maven의 로컬 저장소 위치를 CLI 매개 변수로 지정 (0) | 2020.07.30 |
---|---|
form.submit의 응답을 캡처하는 방법 (0) | 2020.07.30 |
JavaScript에서 HTML 테이블 본문에 행을 삽입하는 방법 (0) | 2020.07.30 |
Gridview가 THEAD를 렌더링하도록하려면 어떻게해야합니까? (0) | 2020.07.30 |
Django 사이트에서 HTML을 PDF로 렌더링 (0) | 2020.07.30 |