【发布时间】:2012-07-04 06:19:20
【问题描述】:
当我尝试访问 iframe 时,我收到了这个错误,我不确定我做错了什么。有人可以帮我解决这个问题吗?
var ifrm = document.getElementById('iframe'),
ifrm = (ifrm.contentWindow) ? ifrm.contentWindow :
(ifrm.contentDocument.document)
? ifrm.contentDocument.document :
ifrm.contentDocument;
ifrm.open();
ifrm.write("Hello World!");
ifrm.close();
这些是我收到的错误:
未捕获的类型错误:无法读取未定义的属性“文档”
未捕获的类型错误:无法读取未定义的属性“readyState”
【问题讨论】:
-
Cannot read property 'document' of undefined 很清楚
-
iframe.contentWindow不是跨浏览器? -
附注:对嵌套的三元运算符说“不”。
-
我假设
ifrm是对 iframe 节点的引用(例如var ifrm = document.querySelector('iframe'))。在这种情况下,您可以拥有:ifrm.contentDocument或ifrm.contentWindow.document。
标签: javascript dom iframe cross-domain