admin管理员组文章数量:1201372
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'?
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.
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'?
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.
Share Improve this question edited Feb 20, 2012 at 4:55 townie asked Feb 20, 2012 at 4:35 townietownie 7872 gold badges8 silver badges12 bronze badges 1- @Anurag No. I saw someone posted an answer saying you can't access that information due to browser security but they deleted it. I guess you can only access the actual iframe part and any content it generates is off limits? – townie Commented Feb 20, 2012 at 4:49
5 Answers
Reset to default 10Use..
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.
Of course, this all relies on the access to the iframe
's document
not violating Same Origin Policy.
getElementsByTagName()
returns a NodeList, which you'll need to iterate over. To iterate over it, you can do:
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:
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.
stuff[0].contentWindow.document.getElementsByTagName('a')
should produce an array-like object containing all the links in the zero-th iframe.
You need to use contentWindow.document
because the <iframe>
does not contain any nodes in the current document -- it is a frame whose contentWindow
property points to a different window with its own document.
getElementsByTagName
returns an array of matching elements. To access an individual IFRAME, you would use stuff[0]
for example.
Why is everyone suggesting contentWindow.document
when you can just use contentDocument
instead?
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
.
本文标签: javascriptgetElementsByTagName(39a39) not workingStack Overflow
版权声明:本文标题:javascript - getElementsByTagName('a'); not working? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738618205a2103033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论