简体   繁体   English

如何在'ul'元素中选择嵌套的DOM元素

[英]How to select nested DOM elements inside 'ul' element

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:@ Oskar

$$("ul.menu ul li ul li a");

@ Dimitar @Dimitar

document.getElements("ul.menu ul li ul li a");

Keep Geeking :) 保持Geeking :)

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 getElementsByTagName on the element: 如果要限制元素内的搜索,请在该元素上设置一个id,以便您可以轻松找到它,并在元素上使用getElementsByTagName

var links = document.getElementById('menu').getElementsByTagName('a');
// 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");

$$('.menu a')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM