admin管理员组文章数量:1332359
I have HTML code structure:
<ul id="main">
<li>
<a href="#"></a>
<ul>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
<li>
</ul>
Want to select all elements inside UL id="main".
Tried to use:
var el = document.getElementById("main").getElementsByTagName("*");
for (var i=0; i<el.length; i++) {
alert(el[i].tagName);
}
But only get LI and A tags. UL tags are missing. Any ideas ?
I have HTML code structure:
<ul id="main">
<li>
<a href="#"></a>
<ul>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
<li>
</ul>
Want to select all elements inside UL id="main".
Tried to use:
var el = document.getElementById("main").getElementsByTagName("*");
for (var i=0; i<el.length; i++) {
alert(el[i].tagName);
}
But only get LI and A tags. UL tags are missing. Any ideas ?
Share Improve this question edited Jul 16, 2010 at 16:54 Bill the Lizard 406k212 gold badges574 silver badges891 bronze badges asked Jun 21, 2010 at 11:20 BounceBounce 2,1056 gold badges35 silver badges66 bronze badges1 Answer
Reset to default 4I get the <ul>
tag with your code, give it a test here: http://jsfiddle/RFKsC/1/ (it's the third alert).
So what you have should work, you do need a /
in your HTML though, this part:
</ul>
<li> <!-- should be </li> -->
</ul>
Without that closing tag, you may get some funky/unpredictable cross-browser behavior, fixing it should resolve the issue.
本文标签: javascriptGet all elements nested in UL tagStack Overflow
版权声明:本文标题:javascript - Get all elements nested in UL tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742316693a2451955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论