首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2012-03-30 
用JS动态生成iframe,内容无法显示先贴代码JScript codevar body document.bodyvar iframe document.c

用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为单线程

热点排行