jQuery - select subset of elements within a saved

2019-01-19 19:33发布

问题:

From this:

var $saved = $('#parent').find('a');

How can I now subselect those elements in $saved which have class myClass?

I don't want descendents (so find or children are no good), I want the subset of $saved.

var $refined = $saved.[something something];

Essentially, I want $refined to be equal to $('#parent').find('a.myClass'); but to start from $saved.

Thanks.

回答1:

You can use filter method:

var $refined = $saved.filter(".myClass");


回答2:

You can use filter method.

var $refined = $saved.filter('.myClass');


回答3:

may be this one can help

var $refined = $saved.filter('.myClass');

http://api.jquery.com/filter/



回答4:

You can iterate through saved collection to find out the elements with class myClass.

var $refined  = $saved.each(function(){
   if($(this).attr('class') == 'myClass')
      return $(this);
});

Or you can use filter() jquery function to apply selector.

 var $refined  = $saved.filter('myClass');


回答5:

$saved.each(function(){
            if($(this).hasClass('test')) alert($(this).text());
        });