简体   繁体   中英

How would I get multiple "li" elements under a specific "ul" element and put them into an array?

<div id="a", class="1">
  <div class="2">...</div>
  <ul id="b", class="3">
    <li class="4">...</li>
    <li class="4">...</li>
    <li class="4">...</li>
    <li class="4">...</li>
  </ul>
  <div class="5">...</div>
  <div class="6">...</div>
</div>

This is some existing code on the site I'm making my extension for and what I want is I want to create an array with all the li elements. So for example if there was 7 li elements my array would have all 7 inside the array, if there was 5 the array would have 5 li elements.

I had a go of the answers over here but the answer by Burak put the 3 div's surrounding the ul into my array and the answer by Antonio put nothing into my array.

A solution is to use

document.querySelectorAll('#b li');

Wich will select all li in the element that has id="b"

You'll found useful docs here https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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