简体   繁体   English

jQuery - 使用inArray()查找jQuery对象的索引

[英]jQuery - using inArray() to find index of jQuery object

I have a few divs that I'd like to put into an array. 我有几个div,我想把它放入一个数组。

When I try to use jQuery.inArray(), my div (as a jQuery object) isn't found. 当我尝试使用jQuery.inArray()时,找不到我的div(作为jQuery对象)。 Why not? 为什么不?

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

alert(jQuery.inArray($("#div1"),myArray)); // returns -1

$ 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. $每次都会创建一个新的jQuery 集合对象,因此var a = $("div"), b = $("div")实际上是两个彼此不相等的不同对象。

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. 但它确实取决于您的用例。

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

You can't use .inArray() for object comparison by content. 您不能使用.inArray()按内容进行对象比较。

I like the approach outlined here . 我喜欢这里概述的方法。 It's very clean. 它非常干净。

It's probably because each invocation of the jQuery constructor results in a new instance referring to the same DOM node. 这可能是因为每次调用jQuery构造函数都会导致引用同一DOM节点的新实例。 What would effectively allow you to demonstrate inArray looks something like this: 什么能有效地让你演示inArray看起来像这样:

var $div1 = $('#div1'), 
    $div2 = $('#div2'), 
    myArray = [ $div1, $div2 ];

alert(jQuery.inArray($div1,myArray));

You are not storing any references to the jQuery objects, $("#div1") will return a new jQuery object containing your dom element, you are comparing two different jQuery objects containing the same dom element. 您没有存储对jQuery对象的任何引用,$(“#div1”)将返回包含您的dom元素的新jQuery对象,您正在比较包含相同dom元素的两个不同的jQuery对象。 inArray will work just fine if you are using the same reference in the array as when you do use the inArray method. 如果在数组中使用与使用inArray方法时相同的引用,inArray将正常工作。

var arr = [],
    $d1 = $("#d1"),
    $d2 = $("#d2"),
    $d3 = $("#d3");

arr.push($d1, $d2, $d3);

console.log(jQuery.inArray($d3, arr));

or see http://jsfiddle.net/EQQ96/2/ 或者参见http://jsfiddle.net/EQQ96/2/

You're better off creating an array of ids. 你最好创建一个id数组。 When it you roll, you can then see if that id is in your array, and then move forward. 滚动时,您可以查看该ID是否在您的数组中,然后继续前进。

var possiblePositions = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

function randomSpin(sides) {
 return Math.floor(Math.random() * (sides || 6) ) + 1;
}
var $currentPiece = $('piece.active');
var currentSpot = $currentPiece.attr('spotPosition');
var spin = randomSpin(6) + randomSpin(6);
var nextSpot = currentSpot + spin;

if (possiblePositions.indexOf(nextSpot)) {
    $('#div' + nextSpot).append($currentPiece);
}

If you were to use purely jQuery to manipulate the jQuery Collection then you can use the jQuery index() method. 如果您使用纯jQuery来操作jQuery Collection那么您可以使用jQuery index()方法。 However, the index returned is the position of the element in the dom, not the order in which it was added to the collection. 但是,返回的索引是dom中元素的位置,而不是它添加到集合中的顺序。 If you need to deal with the order of adding then you're better of using selector strings rather than jQuery Collection : 如果你需要处理添加的顺序,那么你最好使用selector strings而不是jQuery Collection

var myArray = $([]).add( "#div4" ).add( "#div3" ).add( "#div1" ).add( '#div2' );
console.log( myArray.index( $('#div3') ) ); //Output: 2

JS FIDDLE DEMO JS FIDDLE DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM