Javascript 如何在“ul”元素中选择嵌套的 DOM 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3468082/
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
How to select nested DOM elements inside 'ul' element
提问by Q_Mlilo
I am looking for a way to collect all <a>tags and load then in an array using Mootool 1.1 or pure javascript.
我正在寻找一种方法来收集所有<a>标签,然后使用 Mootool 1.1 或纯 javascript 加载到数组中。
<ul class="menu">
<li>
<ul>
<li><a href="#">Group One</a>
<ul>
<li><a href="#">I want</a></li>
<li><a href="#">I want too</a></li>
</ul>
</li>
<li><a href="#">Group Two</a>
<ul>
<li"><a href="#">I want</a></li>
<li><a href="#">I want too</a></li>
</ul>
</li>
</ul>
</li>
</ul>
Edit Solution:
编辑解决方案:
Thank you all, your responses have helped me find a more precise solution.
谢谢大家,你们的回答帮助我找到了更精确的解决方案。
Mootools 1.1: @ Oskar
Mootools 1.1:@奥斯卡
$$("ul.menu ul li ul li a");
@ Dimitar
@迪米塔尔
document.getElements("ul.menu ul li ul li a");
Keep Geeking :)
保持 Geeking :)
采纳答案by Dimitar Christoff
// for the links of the first ul.menu on the page
var menuLinks = document.getElement("ul.menu").getElements("a");
// or you can get all links children of all uls with class menu
var menuLinks = document.getElements("ul.menu a");
回答by Guffa
I'm not sure if you want to limit the action somehow, but getting all anchor elements in the page is easy:
我不确定您是否想以某种方式限制操作,但是获取页面中的所有锚元素很容易:
var links = document.getElementsByTagName('a');
If you want to limit your search inside an element, set an id on that element so that you can easily find it, and use the getElementsByTagNameon the element:
如果您想限制在元素内搜索,请在该元素上设置一个 id,以便您可以轻松找到它,并getElementsByTagName在该元素上使用:
var links = document.getElementById('menu').getElementsByTagName('a');
回答by Oskar Krawczyk
$$('.menu a')
$$('.menu a')

