javascript iframe.contentWindow.document Chrome 替代品?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5006494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iframe.contentWindow.document Chrome alternative?
提问by Skizit
How would I call the following in Chrome? iframe.contentWindow.document
我将如何在 Chrome 中调用以下内容? iframe.contentWindow.document
回答by Jurij Belous
Chrome does in fact support iframe.contentWindow.document, but there's a little wrinkle that you've probably fallen prey to: if the file set to the iframe.srcproperty is being accessed locally (i.e. using the "file://" protocol), that property is inaccessible in Chrome. This will happen if you specify a relative file address and attempt to test the script without using a web-server like IIS or Apache (by simply double-clicking it). The same also applies to iframe.contentDocument.
Chrome 实际上确实支持iframe.contentWindow.document,但是您可能会遇到一些问题:如果设置为该iframe.src属性的文件正在本地访问(即使用“file://”协议),则该属性在 Chrome 中无法访问. 如果您指定相对文件地址并尝试在不使用 IIS 或 Apache 等网络服务器的情况下测试脚本(只需双击它),就会发生这种情况。这同样适用于iframe.contentDocument.
I ran into a similar problem where, for some odd reason, Chrome would not accept event handlers dynamically attached to iframes. Then I found the note in this article, tested with Apache, and hey presto, it suddenly began to work.
我遇到了类似的问题,出于某种奇怪的原因,Chrome 不接受动态附加到 iframe 的事件处理程序。然后我找到了这篇文章中的注释,用Apache进行了测试,嘿嘿,它突然开始工作了。
回答by Tim Down
iframe.contentDocumentis what you want.
iframe.contentDocument是你想要的。
回答by Aligned
<iframe id="popupFrame" />
<script type="text/javascript">
var popupFrame = document.getElementById('popupFrame');
var iFrameDoc;
if (switchAccountsPopupFrame.document) {
iFrameDoc = switchAccountsPopupFrame.document; // works with IE9 and FF9
}
else {
// Google Chrome (16.0.912.75 m)
iFrameDoc = switchAccountsPopupFrame.ownerDocument;
}
....
</script>

