【问题标题】:iframe.contentWindow.document Chrome alternative?iframe.contentWindow.document Chrome 替代品?
【发布时间】:2011-06-27 17:58:47
【问题描述】:

如何在 Chrome 中调用以下内容? iframe.contentWindow.document

【问题讨论】:

标签: javascript iframe google-chrome


【解决方案1】:

iframe.contentDocument 是你想要的。

另见Chrome: Getting iFrame and inserting into body

【讨论】:

  • 为什么在使用这个的时候.. .defaultView.getComputedStyle of the iframe.contentDocument say ...Uncaught TypeError: Cannot call method 'getComputedStyle' of undefined ?
  • @Skizit:对我来说效果很好。你有测试页面或示例代码来显示问题吗?
  • @Šime:它似乎在当前版本的 Chrome 中工作。我不确定它是否总是如此;我想我在某个地方读到了它没有。 contentDocument 在它存在的地方更可取。
【解决方案2】:
<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>

【讨论】:

  • ownerDocument 听起来就是这样。它是 iFrame 所有者的文档,即。 e. 文档.
【解决方案3】:

Chrome 确实支持iframe.contentWindow.document,但您可能会遇到一些问题:如果设置为iframe.src 属性的文件正在本地访问(即使用“file://”协议),该属性在 Chrome 中是不可访问的。如果您指定相对文件地址并尝试在不使用 IIS 或 Apache 之类的 Web 服务器(只需双击它)的情况下测试脚本,就会发生这种情况。这同样适用于iframe.contentDocument

我遇到了类似的问题,由于某种奇怪的原因,Chrome 不接受动态附加到 iframe 的事件处理程序。然后我在this article找到了这个note,用Apache测试过,嘿嘿,突然就开​​始工作了。

【讨论】:

    猜你喜欢
    • 2015-11-14
    • 2011-05-07
    • 1970-01-01
    • 2014-04-29
    • 2011-06-10
    • 2014-04-18
    • 2012-04-08
    • 2012-07-09
    • 2011-03-31
    相关资源
    最近更新 更多