Geo*_*uer 2 html iframe specifications internet-explorer-10
我创建了一个页面,上面有一个空的iframe.然后,我可以选择iframe文档并导航到它的正文:
var iframe = document.getElementsByTagName('iframe')[0];
var doc = iframe.contentDocument || iframe.contentWindow.document;
var body = doc.body;
console.log("Body is", body);
Run Code Online (Sandbox Code Playgroud)
在firefox和chrome中,这给了我身体对象.在IE10中它给我null.
这是一个证明这个问题的Jsbin.打开JS,Console,Output面板,然后单击"Run With JS".
两个问题:
小智 6
我今天早些时候遇到过类似的问题.似乎IE,至少9和10,没有正确创建iframe体(当我使用开发人员工具时,我能够在iframe中看到一个body标签,但就像你无法调用它一样),当没有指定的src.它给你null因为它不存在.
是否有跨浏览器方式来访问iframe的正文的答案是否定的.但是,你可以使用一种解决方法.首先,检查iframe主体是否存在,如果不存在,则创建它.
您的代码如下所示:
var iframe = document.getElementsByTagName('iframe')[0];
var doc = iframe.contentDocument || iframe.contentWindow.document;
// The workaround
if (doc.body == null) { // null in IE
doc.write("<body></body>");
}
var body = doc.body;
console.log("Body is", body);
Run Code Online (Sandbox Code Playgroud)
资料来源:http://forums.asp.net/t/1686774.aspx/1