如果它有某个类,则忽略DIV

时间:2022-06-21 19:42:23

I have a function that finds all the DIVS with a class of boxes. It then removes the class of visible.

我有一个函数可以找到所有带有一类盒子的DIVS。然后删除可见类。

I now want to make my function ignore any DIV if it has a class of important.

我现在想让我的函数忽略任何DIV,如果它有一个重要的类。

So basically if my DIV only has the classes of boxes and visible, visible will be removed. But if my DIV has the classes of boxes, visible and important, this DIV will be left alone and visible will not be removed.

所以基本上如果我的DIV只有框的类和可见,那么可见的将被删除。但是如果我的DIV具有可见且重要的盒子类别,那么这个DIV将保持不变并且不会被删除。

$(this).find('.boxes').removeClass('visible');

I know this can be done in an if statement, but I was wondering if there is a better way to do this using jquery?

我知道这可以在if语句中完成,但我想知道是否有更好的方法使用jquery来做到这一点?

5 个解决方案

#1


4  

You can use :not(anotherSelector) in the selector:

您可以在选择器中使用:not(anotherSelector):

$(this).find('.boxes:not(.important)').removeClass('visible');

Another way would be using .not():

另一种方法是使用.not():

$(this).find('.boxes').not('.important').removeClass('visible');

#2


5  

You want to use the :not() selector:

您想使用:not()选择器:

$(this).find('.boxes:not(.important)').removeClass('visible');

#3


2  

$(this).find('div.boxes:not(.important)').removeClass('visible');

#4


1  

$(this).find('.boxes:not(.important)').removeClass('visible');

#5


1  

Use the :not() pseudo-selector:

使用:not()伪选择器:

$(this).find(".boxes.visible:not(.important)").removeClass("visible");

#1


4  

You can use :not(anotherSelector) in the selector:

您可以在选择器中使用:not(anotherSelector):

$(this).find('.boxes:not(.important)').removeClass('visible');

Another way would be using .not():

另一种方法是使用.not():

$(this).find('.boxes').not('.important').removeClass('visible');

#2


5  

You want to use the :not() selector:

您想使用:not()选择器:

$(this).find('.boxes:not(.important)').removeClass('visible');

#3


2  

$(this).find('div.boxes:not(.important)').removeClass('visible');

#4


1  

$(this).find('.boxes:not(.important)').removeClass('visible');

#5


1  

Use the :not() pseudo-selector:

使用:not()伪选择器:

$(this).find(".boxes.visible:not(.important)").removeClass("visible");