How can I apply jQuery
$('.test:not(:has(.example))')
-or-
$('.test').not(':has(.example)')
Possibly
$('.test').filter(function() { return !$(this).children('.example').length; });
This filters out any elements that have any child that matches .example
. If you want to filter based on descendants (not just children) that you can substitute .find
for .children
.
$(':not(.test:has(.example))').css('color', 'red');
http://jsfiddle.net/9fkz7y1g/
if (!$('#yourDiv').children().hasClass("className")) {
//i.e. yourDivID' has no any children whose class name =>'className'
}
jQuery contains()
:
jQuery.contains(document.documentElement, document.body); // true
jQuery.contains(document.body, document.documentElement); // false
This problem seems ready-made for the filter
function where you find all the .test
objects and then when filtering retain only the ones that don't have .example
in them:
$(".test").filter(function() {
return($(this).find(".example").length == 0);
});