Suppose I have select something:
var $nodeAll = $('.sortable');
then I want select some from them,and add them to another variable named $node,what I want select is the parent don’t have a class named ‘node’:
$nodeAll.each(function () {
if ($(this).parents('.node').length == 0) {
//var $node=? what should I do?
}
});
thank you
First off, you can’t have more than one element with an
id="sortable", so I will assume that you’re going to change that to aclass="sortable".You use either jQuery
.filter()with a custom filter function or you can use.not().Using
.not():Using
.filter()with a custom function:If you know that the
.sortableitem will never have.nodeitself, then this is a little more efficient than the previous version as.closest()can be more efficient than.parents(), but.closest()will look on the starting node first, whereas.parents()starts with the first parent and doesn’t look at the current node first: