Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Iframe文档全局_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript Iframe文档全局

Javascript Iframe文档全局,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,这是我几个月来遇到的第一个js/HTML问题,我一直无法在网上找到答案。如果我使用jquery或纯JavaScript动态填充iframe(因此我猜与父文档的来源相同),并且我在主体中使用“document”全局脚本(即“$(document).ready)填充子iframe,我如何确保它访问子文档而不是父文档?如果我用以下HTML填充动态iframe: <iframe id='if'><iframe> 并在脚本标记中添加了以下函数: $(function () {

这是我几个月来遇到的第一个js/HTML问题,我一直无法在网上找到答案。如果我使用jquery或纯JavaScript动态填充iframe(因此我猜与父文档的来源相同),并且我在主体中使用“document”全局脚本(即“$(document).ready)填充子iframe,我如何确保它访问子文档而不是父文档?如果我用以下HTML填充动态iframe:

<iframe id='if'><iframe>

并在脚本标记中添加了以下函数:

$(function () {
    var innerDoc = $($('#if').contents()[0].childNodes[0]);
    innerDoc.html("<scr" + "ipt>" + 
      "var h1 = document.createElement('h1'); " + 
      "h1.innerText = 'hi!';" +  
      "document.body.appendChild(h1);" +
    "</scri" + "pt>"); 
});
$(函数(){
var innerDoc=$($('#if').contents()[0].childNodes[0]);
innerDoc.html(“+
“var h1=document.createElement('h1');”+
“h1.innerText=‘嗨!’;”+
“document.body.appendChild(h1);”+
""); 
});
HTML将被插入到父文档的主体中。我确定这是因为脚本标记中动态插入iframe的“document”变量引用了父文档。如何确保准确的语句正确执行,如中所示,“document”变量将引用子iframe的文档

显然,使用open()和writeln()将使它按预期工作-但我不知道为什么

 var iframedoc = $('#if').contents()[0];
 iframedoc.open();
 iframedoc.writeln("<html><head></head><body><scr" + "ipt>" + 
  "var h1 = document.createElement('h1'); " + 
  "h1.innerText = 'hi!';" +  
  "document.body.appendChild(h1);" +
"</scri" + "pt></body></html>");
iframedoc.close();
var-iframedoc=$('#if').contents()[0];
iframedoc.open();
iframedoc.writeln(“+
“var h1=document.createElement('h1');”+
“h1.innerText=‘嗨!’;”+
“document.body.appendChild(h1);”+
"");
iframedoc.close();
显然,使用open()和writeln()将使其按预期工作-但我不知道为什么

 var iframedoc = $('#if').contents()[0];
 iframedoc.open();
 iframedoc.writeln("<html><head></head><body><scr" + "ipt>" + 
  "var h1 = document.createElement('h1'); " + 
  "h1.innerText = 'hi!';" +  
  "document.body.appendChild(h1);" +
"</scri" + "pt></body></html>");
iframedoc.close();
var-iframedoc=$('#if').contents()[0];
iframedoc.open();
iframedoc.writeln(“+
“var h1=document.createElement('h1');”+
“h1.innerText=‘嗨!’;”+
“document.body.appendChild(h1);”+
"");
iframedoc.close();

您将不得不显示更多的代码。对我有用。你必须展示更多的代码。对我有用。