I want to select list items that are immediate children of #nav, that do not themselves have immediate children with an 'active' class.
This is what I think it should be but it does not work:
$('#nav > li:not(> a.active)')
Given a jQuery object that represents a set of DOM elements, the .children() method allows us to search through the children of these elements in the DOM tree and construct a new jQuery object from the matching elements.
The :empty CSS pseudo-class represents any element that has no children.
Is it possible to select an element if it contains a specific child element? Unfortunately not yet. The CSS2 and CSS3 selector specifications do not allow for any sort of parent selection.
nth-of-type() Selector: This selector is used to select all elements that are the nth-child of their parent in relation to siblings with the same element name. only-child Selector: This selector is used to select all elements that are the only child of their parent.
I really like Ken's solution, but just for an alternative take.
You could add the active class to your list items instead of your links inside. Then your selector could look like:
$("ul#nav li:not(.active)");
If you want to style the links based on the active class, your CSS could look like this:
#nav li.active a{background-color:red;}
This is how you do it:
$('#nav > li:not(:has(a.active))')
You'll need to use jQuery's filter
function:
$('#nav > li').filter(function() { return !($(this).children().is('.active')); })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With