开发者

How to find indexOf element in jQuery array?

开发者 https://www.devze.com 2023-04-12 20:31 出处:网络
I have two selectors var allNodes = $(\"a.historyEntry\"); var errorNodes = $(\"a.historyEntry.error\");

I have two selectors

    var allNodes = $("a.historyEntry");
    var errorNodes = $("a.historyEntry.error");

I would like to to find a node before first error node, so I need to find an index of first error node, how to do it?

I tried to use inAr开发者_StackOverflow中文版ray method, but it doesn't work for this

$.inArray(allNodes, errorNodes.first())

or

$.inArray(allNodes, $(errorNodes.first()))

Is there any fast way to do it in jQuery or do I have to use for loop?


index()?

It's like indexOf... but just without the Of... it returns the index of the element if it exists, and -1 if it doesn't.


Use index(). It does exactly the same thing as indexOf in java.


$.inArray value is the first parameter then the array:

$.inArray(allNodes, errorNodes.first())

should be:

$.inArray(errorNodes.first(), allNodes)

Example

0

精彩评论

暂无评论...
验证码 换一张
取 消