简体   繁体   English

jQuery中的addClass不能按我的要求工作

[英]addClass in jquery doesn't work as I want

I have a code that adds a class ( .class2 ) using jQuery. 我有一个代码,使用jQuery添加了一个类( .class2 )。 The class is applied, but some styles of the class don't work : 应用了该类,但是该类的某些样式无效:

$('.item1').addClass('class2');    
.item1 {
    font-size: 40px;
    text-align: center;
    height: 60px;
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 0px 0px;
    text-decoration: none;
    color: white;
    margin: -10px 0px;
    -webkit-transition: background-color 0.4s;
    -moz-transition: background-color 0.4s;
    transition: background-color 0.4s;
}
.item1:hover {
    background-color: rgba(0, 0, 0, 0.7);
}
.class2 {
    background-color: rgba(0, 0, 0, .85);
    border: 1px solid black;
    cursor: default;
    /* background-color and border aren't applied */
}

That because .item1 has already a background-color and override the new background-color added by new class .class2 , so you could try to make your rule more specific like : 那是因为.item1已经具有background-color并覆盖了新类.class2添加的新background-color ,因此您可以尝试使规则更具体,例如:

.item1.class2 {
   ...
   background-color: rgba(0, 0, 0, .85);
}

Hope this helps. 希望这可以帮助。

The problem isn't on jQuery. 问题不在jQuery上。 The problem is with your CSS rules. 问题出在您的CSS规则上。

Explanation 说明

The browser always will apply the rule that is more specific, eg: 浏览器将始终应用更具体的规则,例如:

HTML: HTML:

<div id="my-div">
   <ul class="my-list">
      <li class="my-item">
   </ul>
</div>

CSS: CSS:

#my-div > ul > li.my-item {
    background-color: none;
}

.my-item {
    background-color: red
}

The browser will check what rule is more specific, in this case, the first one is clearly more specific. 浏览器将检查哪个规则更具体,在这种情况下,第一个规则显然更具体。

If you want to override this rule, you must write a rule with at least the same specificity (Check this specificity calculator) . 如果要覆盖此规则,则必须编写至少具有相同特异性的规则(检查此特异性计算器)

If the rules have the exact same specificity, the last one will be applied. 如果规则具有完全相同的特异性,则将应用最后一个规则。

Solution

Use your browser developer tools to check what rule is being applied on your element, then write a rule with at least equal specificity. 使用浏览器开发人员工具检查在元素上应用的规则,然后编写至少具有相同特异性的规则。

There's no a exact solution, since the entire page should be analysed. 由于应分析整个页面,因此没有确切的解决方案。

Why not !important? 为什么不重要?

First let's understand how !important works. 首先,让我们了解!important工作原理。 !important will make your rule have the highest specificity, like an "infinite" specificity. !important将使您的规则具有最高的特异性,例如“无限”的特异性。

So that rule only can be override for a rule after this one, also with !important. 因此,该规则只能被此规则之后的规则覆盖,也要使用!important。

Okay, this works, solve my problem, right? 好的,这可以解决我的问题,对吗?

Although this solve your problem right now, this practice isn't good, and will give you a very hard to maintain CSS. 尽管这可以立即解决您的问题,但是这种做法并不好,并且会给您带来非常难以维护的CSS。

If you use it on everything, you is just killing the C of CSS (Cascading Style Sheets), and you will need to rely only on rules orders, and sometimes you will just not be able to write a rule that you want. 如果在所有内容上都使用它,那么您将扼杀CSS C (级联样式表),并且您将仅需要依赖规则顺序,有时您将无法编写所需的规则。

Everyone that started using CSS thinks that the Cascade is a boring thing, that using !important on everything is fine, but isn't. 刚开始使用CSS的每个人都认为Cascade是一件无聊的事情,在所有内容上都使用!important是可以的,但事实并非如此。 Soon or later you will realize. 迟早您会意识到的。

You're code is working for me (see below). 您的代码正在为我工​​作(请参见下文)。 Check your developer console if you're styles are overwritten by other rules. 如果您的样式被其他规则覆盖,请检查您的开发者控制台。 In this case, use a higher specifity , eg .item1.class2 to overwrite your already set background-color. 在这种情况下,请使用更高的 .item1.class2 ,例如.item1.class2来覆盖已经设置的背景颜色。

!important isn't a solution , but a workaround at best (it's bad practice). !important不是解决方案 ,但充其量只是一种解决方法(这是不好的做法)。 Try to organize your rules to have the proper order. 尝试组织您的规则以拥有正确的顺序。

 // JS with jQuery $('.item1').addClass('class2'); 
 .item1 { font-size: 40px; text-align: center; height: 60px; background-color: rgba(0, 0, 0, 0.5); border-radius: 0px 0px; text-decoration: none; color: white; margin: -10px 0px; -webkit-transition: background-color 0.4s; -moz-transition: background-color 0.4s; transition: background-color 0.4s; } .item1:hover { background-color: rgba(0, 0, 0, 0.7); } .item1.class2 { background-color: rgba(0, 0, 0, .85); border: 1px solid black; cursor: default; /* background-color and border aren't applied */ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="item1"></div> 

If you load jquery library, and still doesn's work. 如果您加载jquery库,仍然无法正常工作。 You can add !important to style so can ovveride the style, here is example of code at class2. 您可以在样式中添加!important,以便可以使用样式,这是class2的代码示例。

.class2 {
    background-color: rgba(0, 0, 0, .85) !important;
    border: 1px solid black !important;
    cursor: default !important;
    /* background-color and border aren't applied */
}

javascript jquery javascript jquery

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

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