jQuery的 - 用inArray()来查找jQuery对象的索引 [英] jQuery - using inArray() to find index of jQuery object

查看:162
本文介绍了jQuery的 - 用inArray()来查找jQuery对象的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想放入数组数的div。

当我尝试使用jQuery.inArray(),我格(作为一个jQuery对象)没有找到。为什么不呢?

  myarray的无功= [$(#DIV1)$(#DIV2)$(#DIV3)];警报(jQuery.inArray($(#DIV1),myArray的)); //返回-1


解决方案

$ 创建一个新的jQuery的的每一次反对,所以 VAR一个= $(格),b = $(格)实际上是不相等的两个不同的对象对方。

相反,你可以只使用选择或元素的一些其他识别特征。

  myarray的无功= [#DIV1,#DIV2,#DIV3];

然而,它真的取决于你的使用情况。

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 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.

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.

这篇关于jQuery的 - 用inArray()来查找jQuery对象的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆