读书人

用JS动态生成iframe,内容无法显示,该如

发布时间: 2012-03-30 17:32:09 作者: rapoo

用JS动态生成iframe,内容无法显示

先贴代码

JScript code
var body = document.body;var iframe = document.createElement("iframe");iframe.id = "one";body.appendChild(iframe);var h = document.createElement("h1");h.innerHTML = "what's the fuck what's the fuckwhat's the fuckwhat's the fuckwhat's the fuckwhat's the fuckwhat's the fuckwhat's  the fuckwhat's the fuckwhat's the fuckwhat's the fuckwhat's the fuckwhat's the fuck";alert("1");// var fbody = document.getElementById("one");var fbody1 = iframe.contentWindow.document.body;fbody1.appendChild(h);



在火狐上运行...
当alert("1")注销掉的时候...
h标签的文本内容看不到...
用firebug查看的话...
如果alert("1")注销了...
h1标签是没有的...
请问这是什么问题...???

[解决办法]
如果没有alert()的话并非没有h中的文本,而是此时h中的内容让iframe的src指向覆盖了,当你用了alert()的时候正好起到一个线程阻塞的作用,此时src属性要先与h中的内容,这时候是h把src中的内容覆盖了。个人猜想,因为js为单线程

读书人网 >JavaScript

热点推荐