【发布时间】: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