How to iterate through ul list using Javascript?

后端 未结 4 1283
闹比i
闹比i 2020-12-28 18:54

I have the following HTML page (page is simplified here as it is a sample of the real one):





        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 19:50

    No. Getting links by getElementsByTagName("a") is your one-off web-developer solution.

    You can also traverse the DOM properly by childNodes, and this solution generalizes to all UL lists you may have:

    _($("#my-list")[0].childNodes).filter(function(node) { return node.nodeName == "LI"; })
    

    It uses underscore and jQuery.

提交回复
热议问题