Why doesn't index() working as expected

前端 未结 5 1655
余生分开走
余生分开走 2021-01-22 18:09

HTML:

Div 1
Div 2
Div 3
<
5条回答
  •  抹茶落季
    2021-01-22 18:43

    You have to make it relative to what you're testing against. Use:

    $(".test").each(function(){
        var i = $('.test').index($(this));
        console.log(i);
    });
    

    jsFiddle example

    Per the docs on .index():

    If .index() is called on a collection of elements and a DOM element or jQuery object is passed in, .index() returns an integer indicating the position of the passed element relative to the original collection.

提交回复
热议问题