简体   繁体   中英

How to choose the elements from the jQuery object

I'm sorry for stupid title.

Problem: I need to select only .special element from $elements variable.

<div class="element"></div>
<div class="element special"></div>
<div class="element"></div>

Jquery:

var $elements = $('.element'),
    $special  = $element.find('.special'); // Not working.

UPD: Please, don't write something like "$('.element.special')". It's wrong for my case.

You need to use .filter()

var $elements = $('.element'),
    $special  = $element.filter('.special');

Since the element you are targeting is a member of the $element set, you need to use .filter() , .find() is used to find descendant members of the elements in the calling set

< div class="element">bbb< /div>
< div class="element special">aaa< /div>
< div class="element">aaa< /div>

var $special = $('.element.special');
$special.html("test");

您可以使用

var $special = $('.element.special');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM