About JS traversing ul li

 Structure 
<div id ="a">
<ul>
<li></li>

<li></li>
<li></li>
<li></li>
<li></li>
<li> </li> 
</ul>

</div>

Under the condition that the structure remains unchanged,
I click the corresponding position of each li display, such as the first display 1, the second 5 display 5
$('#a').find('li').each(function(){
$(this).click(function(){
alert($(this).index())
})
})
Don't forget to import jquery
Post a pure js version again
<div id="a">
    <ul>
        <li>111111111</li>
        <li>2222222222</li>
        <li>33333333</li>
        <li>4444444</li>
        <li>55555555</li>
        <li>66666666</li>
    </ul>
</div>
<script>
  var cNode =document.getElementById('a').getElementsByTagName('li');
        for( var i=0; i<cNode.length; i++){
            cNode[i].index= i;
            cNode[i].onclick = function(){
                alert( "this index of cNode is : " + this.index);
            }
        }
</script>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325701172&siteId=291194637