Find the position of an element within a list

后端 未结 4 749
太阳男子
太阳男子 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:42

    Use index():

    $("li").click(function() {
      var index = $(this).parent().children().index(this);
      alert("You clicked item " + index);
    });
    

    Indexes start at 0.

提交回复
热议问题