jQuery Selectors - where item does not have children with a certain class

后端 未结 4 385
南方客
南方客 2021-01-17 22:39

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 sho

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 23:04

    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;}
    

提交回复
热议问题