Suppose I have the following HTML:
- One
- Two
You can do it like this
$("li").not('.foo li')
http://jsfiddle.net/y7s54/
or
$("li:not(.foo li)")
http://jsfiddle.net/QpCYY/
Select all li's that don't have an ancestor with class foo
I think this solution looks a little nicer, but there's a bit of controversy on the API comments about speed differences.
$("li").not(".foo li")
fiddle
How about this:
$("li:not(.foo > li)")
The comments in the docs are quite useful if this doesn't work:
http://api.jquery.com/not-selector/
Try li:not(.foo > ul > li)
; that selects all lis minus those with a parent .foo two levels up.
You can do it using context along with selector as under,
$('li', $('div:not(.foo)'))
LIve Demo
$('li', $('div:not(.foo)')).each(function(){
alert($(this).text());
});