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

后端 未结 7 1370
粉色の甜心
粉色の甜心 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:37

    $ creates a new jQuery collection object each time, so var a = $("div"), b = $("div") will actually be two different objects that don't equal each other.

    Instead you can just use the selectors or some other identifying feature of the element.

    var myArray = ["#div1", "#div2", "#div3"];
    

    However it really depends on your use case.

提交回复
热议问题