Finding a DOM node in the subtree of a node?
I have HTML with something like
<div class="foo">
<h1>foo 1</h1>
..</div>
<div class="foo">
<h1>foo 2</h1>
..</div>
Finding the div nodes using getElementsByTagName('开发者_开发问答div') is not a problem.
While iterating of the div nodes I need to find the first H1 inside the subtree of the DIV nodes. Is there something like getElementByTagName() on a DOM node?
No. Use array notation to get the first element found:
var firstHeading = div.getElementsByTagName('h1')[0];
Edit If you were asking if you could use getElementsByTagName
on nodes other than the document
, yes you can. All DOM element nodes plus the document
node have the getElementsByTagName
method.
精彩评论