【问题标题】:In IE, when binding to the 'load' event of an IFRAME, this.contentDocument is undefined在 IE 中,当绑定到 IFRAME 的“加载”事件时,this.contentDocument 未定义
【发布时间】:2011-04-27 18:00:47
【问题描述】:

好吧,伙计们,请耐心等待,这个设置很长。

我有一个简单的页面。它加载一个 iframe。该 iframe 内部是一个表单。我希望 iframe 中的表单通过 jQuery 与父页面交互。

这在 Firefox、Chrome 和 Safari 中可以正常工作。在这里自己看看:

http://dl.dropbox.com/u/58785/iframe-example/index.htm

但是,在 Internet Explorer 6/7/8/9 中,它不起作用。 load 事件触发,但 jQuery 无法获取 iframe 内元素的句柄。

我正在使用 jQuery 函数的第二个 'context' 参数来设置选择器的上下文,如下所示:var form = $('#myform'), this.contentDocument)

这就是batty。使用 IE9 中的 F12 开发人员工具,我可以在我的 JavaScript 中设置断点并查看 IE 如何评估 JavaScript。如果我将鼠标悬停在this 上,我可以看到它确实有一个contentDocument 属性。 但是,如果我将鼠标悬停在 this.contentDocument 上,它会告诉我它是未定义的。

因为它是未定义的,所以 jQuery 选择器不返回任何元素。同样,这仅在 IE 中。而且 IFRAME 在同一个域上,所以不是同源问题。

任何指针?

【问题讨论】:

  • 你试过contentWindow吗?见stackoverflow.com/questions/1477547/…
  • @Roatin Marth - 是的,this.contentWindow.document 解决了所有问题。您能否将其发布为答案,以便获得您应得的代表?

标签: javascript jquery internet-explorer iframe


【解决方案1】:

不要践踏 Roatin 的回答,但这个问题也可以通过指定 DOCTYPE 声明来解决。 Internet Explorer 8 及更高版本要求它用于 contentDocument。否则,正如他所说,可以使用 contentWindow(也适用于早期版本的 IE)。见the information at W3Schools

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多