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

后端 未结 3 1615
感动是毒
感动是毒 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 <li> 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).

    0 讨论(0)
  • 2021-01-23 19:26

    this inside the callback function is just what you want, the clicked item.

    0 讨论(0)
  • 2021-01-23 19:27

    You want to get index of selected ul element? Try this:

    var parentInd = $(this).parent().parent().index();

    0 讨论(0)
提交回复
热议问题