我像这样动态生成 iFrame
var iframe=document.createElement('iframe');
document.body.appendChild(iframe);
var iframedoc=iframe.contentDocument||iframe.contentWindow.document;
iframedoc.body.innerHTML="HI";
fiddle :http://jsfiddle.net/Pbj7S/
它适用于 Google Chrome、Opera、Safari,但不适用于 Firefox。
知道为什么吗?
最佳答案
这有效:
var iframe=document.createElement('iframe');
document.body.appendChild(iframe);
setTimeout(function(){
var iframedoc=iframe.contentDocument||iframe.contentWindow.document;
iframedoc.body.innerHTML="HI";
}, 10);
问题在于您试图在 DOM 中可用之前访问 iframe 文档。
延迟并不重要,重要的是浏览器仅在 js 线程完成工作后才更新显示(以及 Firefox 中的一些 js 可访问对象)。
关于javascript - 在 Firefox 中动态渲染 iFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11331863/