identifying which item is clicked in a multi level list using jquery

后端 未结 3 1659
感动是毒
感动是毒 2021-01-23 18:57

my html looks like this

  • Item 1
  • Item 2
  • Item
3条回答
  •  忘了有多久
    2021-01-23 19:15

    You need to keep track of the list of

  • elements and then use that to determine the index:

    var $all_lis = $('li');
    
    $all_lis.on('click', function() {
      var index = $all_lis.index(this);
      alert(index);
    });
    

    Demo

    The first item will give 0, the last item will give 8 (i.e. 9th item).

提交回复
热议问题