Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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/3/html/78.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中使用document.createElement而不是contentDocument.createElement?_Javascript_Html_Iframe - Fatal编程技术网

Javascript 是否在iframe中使用document.createElement而不是contentDocument.createElement?

Javascript 是否在iframe中使用document.createElement而不是contentDocument.createElement?,javascript,html,iframe,Javascript,Html,Iframe,var-iframe=document.createElement('iframe'); iframe.id='test'; iframe.name='test'; iframe.src='about:blank'; iframe.addEventListener('load',函数(){ var iframeDoc=iframe.contentDocument; var iframeBody=iframeDoc.body; console.log(document==window.top.do

var-iframe=document.createElement('iframe');
iframe.id='test';
iframe.name='test';
iframe.src='about:blank';
iframe.addEventListener('load',函数(){
var iframeDoc=iframe.contentDocument;
var iframeBody=iframeDoc.body;
console.log(document==window.top.document);
var script=document.createElement('script');
script.src=http://example.com/test.js';
script.type='text/javascript';
iframeBody.appendChild(脚本);
},假);

document.body.appendChild(iframe)
为什么要问?我有种感觉这可能会引起一些问题。我只是想知道如果
iframe.contentDocument.createElement
已经起作用,你为什么要知道这一点。使用
document.createElement
有什么好处吗?因为我的同事错误地使用了
document.createElement
,并且它在生产中工作。我好奇地想知道潜在的问题:)你为什么问这个?我有一种感觉,这可能会导致一些问题。我只是想知道如果
iframe.contentDocument.createElement
已经可以工作了。使用
document.createElement
有什么好处吗?因为我的同事错误地使用了
document.createElement
,并且它在生产中起作用。我想好奇地了解潜在的问题:)