【发布时间】:2017-01-15 22:11:50
【问题描述】:
我在 iframe 中有一个脚本,它使用 jquery 查找元素,但是当它运行时,它只找到 iframe 之外的元素,而不是 iframe 内部的元素。
请帮助我将脚本隔离为仅在 iframe 内执行。
我这里有例子:my example
我如何创建 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
【问题讨论】:
-
在此处发布您的代码,而不是作为指向其他网站的链接。
-
等等,什么?你使用 javascript 来附加执行 javascript 的 javascript,为什么?
标签: javascript jquery iframe