Home > other >  jQuery - :not selector that works in css is not working with jquery
jQuery - :not selector that works in css is not working with jquery

Time:10-07

I'd like to think that I'm good at identifying why things aren't working... but I am at a loss.

I added this style to my page to test and make sure I'm getting what I want...

*:not(.myclass):not(.myclass *)
{
    background-color: orange !important;
}

For some reason, these selectors

//I've tried this
$("*:not(.myclass):not(.myclass *)");
//and this
$(":not(.myclass):not(.myclass *)");
//and this
$(":not(.myclass)");
//and this
$("*:not(.myclass)");

select everything, instead of everything minus items with the class myclass and some of its children.

So when I run a custom method on the jQuery objects, it ends up doing it to all of the items instead of the ones with myclass.

Can anyone tell me why, or point me in the right direction. I've looked at several other posts that are related, but they haven't shed enough light on it for me.

CodePudding user response:

The problem is that the selector matches html and body, since they don't have the myclass class. So if you hide it, you're hiding everything. Use

$("*:not(.myclass,body,html):not(.myclass *)")
  • Related