get the nth-child number of an element in jquery

前端 未结 3 1970
天命终不由人
天命终不由人 2021-02-03 20:23

I have a class of multiple \'DIV\' elements and inside it are list of \'p\' elements. See below:

This is content 1&l

3条回答
  •  萌比男神i
    2021-02-03 21:04

    Use the parameter-less version of the .index() method to find the position of the element relative to its siblings:

    $('.container').children('p').hover(function() {
         var index = $(this).index() + 1;
    });
    

    Note that the result of .index() will be zero-based, not one-based, hence the + 1

提交回复
热议问题