最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find indexOf element in jQuery array? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Oct 14, 2011 at 13:29 IAdapterIAdapter 64.7k73 gold badges186 silver badges243 bronze badges 1
  • If I understand you correctly: some of the nodes in allNodes have the error class, and you want to find the last "good" node before the first "error" node. Is that right? – Blazemonger Commented Oct 14, 2011 at 13:32
Add a comment  | 

3 Answers 3

Reset to default 10

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)

  1. 暂无评论