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

javascript - JQuery search in array of object - Stack Overflow

programmeradmin0浏览0评论

Lets say I have this html :

<ul>
<li class="cls">one</li>
<li class="cls active">tow</li>
<li class="cls">here</li>
<li class="cls">after</li>
</ul>

I'm selecting all .cls by this jquery selector : $('.cls') and put them in a variable:

var c=$('.cls');

c is an array of object now. I want to select .active item in this array by jQuery methods. I do know i can use $('.cls.active') but this is not what I'm lookin for. I want to use c. Is there any solution ?

note: c.find('.active') not working, because .find() searching in childs.

Lets say I have this html :

<ul>
<li class="cls">one</li>
<li class="cls active">tow</li>
<li class="cls">here</li>
<li class="cls">after</li>
</ul>

I'm selecting all .cls by this jquery selector : $('.cls') and put them in a variable:

var c=$('.cls');

c is an array of object now. I want to select .active item in this array by jQuery methods. I do know i can use $('.cls.active') but this is not what I'm lookin for. I want to use c. Is there any solution ?

note: c.find('.active') not working, because .find() searching in childs.

Share Improve this question edited Aug 31, 2013 at 8:58 Pejman asked Aug 31, 2013 at 8:39 PejmanPejman 2,6465 gold badges41 silver badges67 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

use filter() instead of find()

find:

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

filter:

Reduce the set of matched elements to those that match the selector or pass the function's test.

c.filter('.active')

Simply use the filter() method of the jQuery object:

var active = c.filter('.active');

JS Fiddle proof-of-concept.

Reference:

  • filter().

Use .filter()

c.filter('.active');

Use .filter()

c.filter('.active');

发布评论

评论列表(0)

  1. 暂无评论