반응형
jQuery로 iframe의 컨텐츠에 액세스하는 방법은 무엇입니까?
jQuery로 iframe의 컨텐츠에 액세스하려면 어떻게 해야 합니까?이렇게 해봤지만 소용이 없더군요.
iframe 내용: <div id="myContent"></div>
jQuery: $("#myiframe").find("#myContent")
액세스 방법myContent?
jquery/javascript와 유사하게 iframe의 내용에 접근하지만 수락된 답변은 제가 찾던 것이 아닙니다.
당신은 사용해야 합니다.contents()방법:
$("#myiframe").contents().find("#myContent")
출처 : https://web.archive.org/web/20180608003357/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의 내용을 숨깁니다(Same Origin Policy).해결 방법은 외부 컨텐츠를 파일에 저장하는 것입니다(예: PHP).
<?php
$contents = file_get_contents($external_url);
$res = file_put_contents($filename, $contents);
?>
그런 다음 새 파일 내용(string)을 가져와 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
반응형
'programing' 카테고리의 다른 글
| 정의되지 않은 간격띄우기를 방지하는 방법 (0) | 2023.09.07 |
|---|---|
| 함수가 받는 키워드 인수를 나열할 수 있습니까? (0) | 2023.09.07 |
| AddChart2 VBA 매크로의 숫자는 무엇을 나타냅니까? (0) | 2023.09.07 |
| 서버에서 htaccess에서 php 버전을 변경하는 방법 (0) | 2023.09.07 |
| jquery를 사용하여 라디오 버튼 선택값을 설정하는 방법 (0) | 2023.09.07 |