admin管理员组文章数量:1336204
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.
4 Answers
Reset to default 5use 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');
本文标签: javascriptJQuery search in array of objectStack Overflow
版权声明:本文标题:javascript - JQuery search in array of object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398452a2467398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论