How to find indexOf element in jQuery array?

懵懂的女人 提交于 2019-12-10 09:43:52

问题


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 inArray 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?


回答1:


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.




回答2:


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




回答3:


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

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

should be:

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

Example



来源:https://stackoverflow.com/questions/7768333/how-to-find-indexof-element-in-jquery-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!