Find the position of an element within a list

后端 未结 4 763
太阳男子
太阳男子 2021-01-12 00:08

I\'m looking to find the position (i.e. the order) of a clicked element within a list using jQuery.

I have:

  • Element 1
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 00:44

    A more efficient version of Cletus' answer, which doesn't require finding parents and children:

    $("li").on("click", function() {
      var index = $(this).index();
      alert(index);
    });
    
    
    • Element 1
    • Element 2
    • Element 3
    • Element 4

提交回复
热议问题