Get all LI elements in array

后端 未结 3 449
礼貌的吻别
礼貌的吻别 2020-12-05 13:03

How can i make JS select every LI element inside a UL tag and put them into an array?

3条回答
  •  粉色の甜心
    2020-12-05 13:48

    You can get a NodeList to iterate through by using getElementsByTagName(), like this:

    var lis = document.getElementById("navbar").getElementsByTagName("li");
    

    You can test it out here. This is a NodeList not an array, but it does have a .length and you can iterate over it like an array.

提交回复
热议问题