jQuery 内容方法不会在 Chrome 中返回 iframe 内容



我在让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"
  .....
  .....

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

最新更新