jQuery - using inArray() to find index of jQuery object

后端 未结 7 1385
粉色の甜心
粉色の甜心 2021-01-19 02:09

I have a few divs that I\'d like to put into an array.

When I try to use jQuery.inArray(), my div (as a jQuery object) isn\'t found. Why not?

var my         


        
7条回答
  •  忘掉有多难
    2021-01-19 02:36

    Two objects are never the same, so when you do

    var object1 = $('#div1');
    var object2 = $('#div1');
    

    even if you have the same element, the objects are not the same

    If you use the same object, it works

    var div1 = $('#div1');
    var div2 = $('#div2');
    var div3 = $('#div3');
    
    var myArray = [ div1, div2, div3 ];
    
    jQuery.inArray( div1 , myArray); // returns 0
    

提交回复
热议问题