javascript getElementsByTagName('a'); 不工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9356226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:23:15  来源:igfitidea点击:

getElementsByTagName('a'); not working?

javascripthtmldom

提问by townie

So I do this:

所以我这样做:

var stuff = document.getElementsByTagName('iframe');

Works fine. Then I want to do this:

工作正常。然后我想这样做:

var other = stuff.getElementsByTagName('a');

but it doesn't work. It ends up being undefined. I can do:

但它不起作用。它最终是未定义的。我可以:

var other = document.getElementsByTagName('a');

and it's fine. Should I not be able to get all the 'a' in 'iframe'?

没关系。我是否应该无法获得“iframe”中的所有“a”?

Edit: So I don't own the iframe and am now under the impression that I can't access anything the iframe generates which meansI guess I'm screwed.

编辑:所以我不拥有 iframe,现在的印象是我无法访问 iframe 生成的任何内容,这意味着我想我被搞砸了。

回答by alex

Use..

利用..

var iframe = document.getElementsByTagName('iframe')[0],
    iframeDoc = iframe.contentWindow.document;

Specification.

规格

Then, you can call getElementsByTagName('a')on iframeDoc. Alternatively, you could access iframeDoc.links, which isn't quite the same but may be what you want.

然后,你可以叫getElementsByTagName('a')iframeDoc。或者,您可以访问iframeDoc.links,这并不完全相同,但可能是您想要的。

Of course, this all relies on the access to the iframe's documentnot violating Same Origin Policy.

当然,这一切都依赖于访问iframedocument不违反同源策略

回答by Timothy Jones

getElementsByTagName()returns a NodeList, which you'll need to iterate over. To iterate over it, you can do:

getElementsByTagName()返回一个 NodeList,您需要对其进行迭代。要迭代它,您可以执行以下操作:

for (var i = 0; i < stuff.length; i++) {   
    var other = stuff[i].getElementsByTagName('a');
}  

However, getElementsByTagName('a')asks for children inside the current document, which is almost certainly not what you want. If you want children inside the document in the frame, you'll need to do:

但是,getElementsByTagName('a')在当前文档中询问孩子,这几乎肯定不是您想要的。如果您希望框架中的文档内有子项,则需要执行以下操作:

for (var i = 0; i < stuff.length; i++) {   
    var other = stuff[i].contentWindow.document.getElementsByTagName('a');
}  

This will work if you have multiple iframes, and in older versions of IE.

如果您有多个 iframe,并且在旧版本的 IE 中,这将起作用。

回答by Mike Samuel

stuff[0].contentWindow.document.getElementsByTagName('a')

should produce an array-like object containing all the links in the zero-th iframe.

应该生成一个类似数组的对象,其中包含第零个 iframe 中的所有链接。

You need to use contentWindow.documentbecause the <iframe>does not contain any nodes in the current document -- it is a frame whose contentWindowproperty points to a different window with its own document.

您需要使用,contentWindow.document因为<iframe>当前文档中不包含任何节点——它是一个框架,其contentWindow属性指向具有自己文档的不同窗口。

回答by casablanca

getElementsByTagNamereturns an array of matching elements. To access an individual IFRAME, you would use stuff[0]for example.

getElementsByTagName返回匹配元素的数组。例如,要访问单个 IFRAME,您可以使用stuff[0]

回答by Niet the Dark Absol

Why is everyone suggesting contentWindow.documentwhen you can just use contentDocumentinstead?

为什么每个人都建议contentWindow.document您何时可以使用contentDocument代替?

Of course, both work, but I prefer to use what I mean. If I want the window, I use contentWindow. If I want the document, I use contentDocument.

当然,两者都有效,但我更喜欢使用我的意思。如果我想要窗口,我使用contentWindow. 如果我想要文档,我使用contentDocument.