简体   繁体   English

如何忽略css类

[英]How to ignore css class

Can a css value be ignored ? 可以忽略css值吗?

I have two divs : 我有两个div:

<div id="div1" class="class1 class2">
</div>

<div id="div2" class="class1">
</div>

Can the css class be applied to just divs which are styled with class1 and if they contain class2 then its ignored ? 可以将css类应用于使用class1设置样式的div,如果它们包含class2,那么它会被忽略吗? So in above divs just div2 is styled with class1 because div1 is also styled with class2. 所以在上面的div中,div2的样式是使用class1,因为div1也是用class2设计的。

You can use the :not selector. 您可以使用:not selector。 Here's an example: 这是一个例子:

CSS CSS

.red:not(.yellow) {
    color: red;
}
.yellow {
    background-color: yellow;
}

HTML: HTML:

<div class = "red yellow">
   Glee's awesome!
</div>
<div class = "red">
   Glee's awesome!
</div>

Demo . 演示

I hope that helped! 我希望有所帮助!

Write like this: 像这样写:

.class1{
  background:red;
}
.class1.class2{
  background:none;
}

你可以使用:not selector:

$('.class1:not(.class2)')

YESSSS, YESSSS,

use !important after the css property 在css属性之后使用!important

for example height:30px !important 例如height:30px !important

if your class1 have height:30px and class2 height:40px then user !important in class2 height 如果你的class1有height:30px和class2 height:40px那么user!important在class2的高度

The way I would handle this with jquery ( since its how you tagged it and if you only ever want the div with just class class1) is to do a check such as 我用jquery处理这个问题的方法(因为它是如何标记它的,如果你只想要只有class class1的div)就是做一个检查,比如

if($('div').class() == 'class1') {


}

This will select only the div where class equals class1 and not the one with class of class1 class2 这将只选择class等于class1的div而不选择class1 class2 class

Great answers here and I just want to add here a little bit: You've to decide here between selectors that seem to do the same but are a little different: 这里有很好的答案,我只想在这里添加一点:你要在这里选择似乎做同样但有点不同的选择器:

  • "Override class1 styles when class2 is present." “当class2存在时覆盖class1样式。” or "I want special styles when both classes are present." 或者“当两个班级都存在时,我想要特殊的风格。”
  • "Apply special styles when class1 is present and class2 is not." “当class1存在且class2不存在时应用特殊样式。”
  • "I really want to suppress all rules of class1 selectors when class2 is present." “当class2存在时,我真的想要压制class1选择器的所有规则。”

You've to decide what exactly your requirements are. 您需要确定您的要求是什么。 There is a difference between these sentences. 这些句子之间存在差异。

In the first case go with CSS selectors like @sandeep posted, because these selectors work like this. 在第一种情况下,使用@sandeep发布的CSS选择器,因为这些选择器的工作方式如下。 The .class1.class2 selector is more specific than .class1 selector and it is choosen over it when both classes are present. .class1.class2选择器比.class1选择器更具体,当两个类都存在时,它选择它。 So you can set there the rules you want to be applied only if both classes are present. 因此,只有当两个类都存在时,您才可以设置要应用的规则。 Collidating rules in the more specific selector have higher priority than in then other and are able to override it. 更具体的选择器中的碰撞规则具有比其他选择器更高的优先级,并且能够覆盖它。

In the second case go with the :not selectors. 在第二种情况下,请使用:not selectors。 These make it possible to apply certain styles only when a class is present and some other is not. 这使得只有当一个类存在而另一些不存在时才能应用某些样式。

For the third case: Your question is pointing at ignoring a css class. 对于第三种情况:你的问题是指向忽略css类。 This is not possible. 这是不可能的。 You can override its rules in a more specific selector or put its rules in a selector not matching certain tags anymore. 您可以在更具体的选择器中覆盖其规则,或将其规则放在不再与某些标记匹配的选择器中。 But you cannot ignore it as long as its there. 但只要它在那里你就不能忽视它。 Therefore you've to remove the class1 from the class attribute of the tag. 因此,您必须从标记的class属性中删除class1 You can achieve this with jQuery though. 你可以用jQuery实现这一点。

 $(".class2").removeClass("class1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM