Why is .index() always returning 0

后端 未结 3 1671
眼角桃花
眼角桃花 2021-01-18 05:23

I\'m confused as to why .index() is returning 0 in this code. Shouldn\'t it return the index of where it\'s found in the array of jquery objects?

3条回答
  •  星月不相逢
    2021-01-18 05:55

    Another reason that .index() could return 0 is if you are using it like so:

    $('matched elements').click(function(){
        console.log($(this).index()); // will be 0 in some cases
    });
    

    Correct way:

    $('matched elements').click(function(){
        console.log($('matched elements').index($(this))); // will be the index within set of matched elements
    });
    

提交回复
热议问题