【问题标题】:jQuery contents method doesn't return iframe contents in ChromejQuery 内容方法不会在 Chrome 中返回 iframe 内容
【发布时间】:2015-05-20 13:55:56
【问题描述】:

我在让 Chrome 使用 jQuery contents() 返回 iframe 的内容时遇到问题。以下代码在 FireFox 中按预期工作,但 Chrome 不返回正文的任何​​内容。 iframe 文档与主文档在同一个域中,并且确实有一些内容。 (我在文档中包含了两次 iframe,使用 .eq(#) 选择器区分每一个,这就是为什么 jQuery.fn.init[2] 显示 2 个元素。)正如我所指出的,这在 FireFox 中有效。

var $iframe_contents = $("iframe").contents();

摘自 Chrome 调试器。

$iframe_contents: jQuery.fn.init[2]  (There are 2 iframes in the document)
0: document
   URL: "about:blank"
   activeElement: body
   all: HTMLAllCollection[3]
   ...
   baseURI: "about:blank"
   ...
   **body: body**
   ...
   baseURI: "about:blank"
   ...
   **childElementCount: 0
   childNodes: NodeList[0]
   children: HTMLCollection[0]**
   classList: DOMTokenList[0]
   ...
   dataset: DOMStringMap
   dir: ""
   ...
  **firstChild: null
  firstElementChild: null**
  hidden: false
  id: ""
  **innerHTML: ""
  innerText: ""**
  ...
  **lastChild: null
  lastElementChild: null**
  link: ""
  localName: "body"
  .....
  .....

【问题讨论】:

  • $("iframe").contents() 返回 iframe 的文档对象,所以既然你得到了 2 个似乎是正确的对象....你能试试$("iframe").contents().find('body').css('background-color', 'red') 什么的吗
  • @Arun - 谢谢,但这不是问题。返回的对象是空的,例如,children: HTMLCollection[0]。我的实际代码确实有一个 find() 方法,在这种情况下,它返回 null。这一切在 Firefox 中运行良好。
  • 我怀疑你可以直接使用 HTMLCollection[0] 。检查一下为什么这不起作用stackoverflow.com/a/22754453/909535

标签: jquery google-chrome iframe


【解决方案1】:

我相信我已经解决了我的问题。 Firefox 的工作方式似乎与其他浏览器不同。它加载 iframe,然后触发 .ready(),而 Chrome、Safari 和 IE 在 iframe 出现在 DOM 之前触发 .ready()。所以对于其他的,等到 iframe 加载完成后再初始化功能模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    相关资源
    最近更新 更多