我正在构建一个 CMS 应用程序,我现在正在为 IE 调试它。但是事情发生了我无法理解。
首先,我有一个包含页面内容的 div。我提取 div 的内容并使用 javascript 创建一个 iFrame 并将其附加到 div。这是为了制作 WYSIWYG 编辑器。然后,当我想将内容添加到 iFrame 时,问题就开始了。由于某种原因,IE 无法访问 iFrame 的主体。我可以提醒 iFrame 的文档,它会给我 [object Document] 或 [object HTMLDocument],但是当我提醒 body 时,我得到 null。
我使用此函数将文档作为 javascript 对象的一部分获取:
iframedoc: function(aID) {
if (document.getElementById(aID).contentDocument){
return document.getElementById(aID).contentDocument;
} else {
var iFrame = document.getElementById(aID);
return iFrame.contentWindow.document;
}
},
这是我第一次调用它的地方:
tas[i].innerHTML = "";
tas[i].appendChild(ta_edit_functions);
tas[i].appendChild(ta_edit);
tas[i].appendChild(ta_result);
alert(ta.iframedoc(ta_edit_id));
ta.iframedoc(ta_edit_id).body.innerHTML = ta_edit_content;
ta.iframedoc(ta_edit_id).designMode = 'on';
在这里,我清空了 div,附加了一个包含所见即所得函数、iFrame 和最终提交表单的文本区域的 div。
我正在 IE 8 和 9 中进行测试。最奇怪的是,当我在 IE 8 中首次调用 iframedoc 之前发出警报时,我突然得到了正文。
如果有人至少能为我指出正确的方向,我将不胜感激。我真的不知道发生了什么。
最佳答案
I'm testing in IE 8 and 9. Weirdest thing is that when I put an alert before first calling iframedoc in IE 8 I suddenly do get the body.
这听起来好像您对浏览器来说速度太快了——当您试图弄乱它时,iframe 可能还没有完全加载,所以添加一个警报可以给它额外的时间。尝试在 iframe 元素上使用 onload
事件来延迟您的一些代码。
关于javascript - 无法在 IE 中访问 iframe 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10678516/