jquery로 요소를 찾는 iframe 내부에 스크립트가 있지만 실행될 때 iframe 내부에있는 요소가 아닌 iframe 외부에있는 요소 만 찾습니다.
iframe 내에서만 실행되도록 스크립트를 격리하도록 도와주세요.
여기에 예가 있습니다. 내 예
연결된 예에 표시된대로 iframe을 만드는 방법 :
//Head to iframe:
var head = '<meta charset="utf-8">' +
'<meta http-equiv="X-UA-Compatible" content="IE=edge">' +
'<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">' +
'<script src="http://code.jquery.com/jquery-1.10.2.js"><\/script>';
//Body to iframe that contains a script that I would like only to run inside the iframe:
var body = '<code id="codeInside">code tag inside</code>' +
'<script>var codeTag = $("code").text("TAG FOUND!!");<\/script>';
//Insert the iframe:
var iframe = $('<iframe src="javascript:\'\'">').appendTo($('body'));
iframe.contents().find('head').html(head); //Append head til the iframe
iframe.contents().find('body').html(body); //Append body to the iframe
나는 그것을 얻었다 : 작동하는 해결책
//Head to iframe:
var head = '<head><meta charset="utf-8">' +
'<meta http-equiv="X-UA-Compatible" content="IE=edge">' +
'<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">' +
'<script src="http://code.jquery.com/jquery-1.10.2.js"><\/script></head>';
//Body to iframe that contains a script that I would like only to run inside the iframe:
var body = '<body><code id="codeInside">code tag inside</code>' +
'<script>var codeTag = $("code").text("TAG FOUND!!"); console.log("codeTag", codeTag);<\/script></body>';
//Insert the iframe:
var iframe = $('<iframe src="javascript:\'\'">').appendTo($('body'));
var doc = iframe[0].contentWindow.document;
doc.open();
doc.write(head);
doc.write(body);
doc.close();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다