开发者

Finding a DOM node in the subtree of a node?

开发者 https://www.devze.com 2023-02-23 12:19 出处:网络
I have HTML with something like <div class=\"foo\"> <h1>foo 1</h1> ..</div> <div class=\"foo\">

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消