1

I've got an element class with the following CSS:

#item-group-navigation li a{
    text-decoration: none;
    color: white;
    font-size: 18px;
    font-family: MyriadProReg;
    vertical-align: -1px;
}

And I add the following class using jQuery after a certain event:

.item-group-selected{
    color:rgb(245, 255, 0);!important
}

But the problem is that the dynamically added style doesn't work. The default style is applied instead. I tried to use the !important keyword but it doesn't work. Can anyone explain what I'm doing wrong?

4

3 回答 3

8

如果使用!important它需要放在分号之前。

color:rgb(245, 255, 0) !important;

不过,您不需要使用它。确保在可能覆盖该颜色的任何其他样式之后添加您的类,并检查您的页面以查看当前颜色的计算来源。

例如,如果您的 CSS 如下所示:

.item-group-selected{
    color:rgb(245, 255, 0);
}

#item-group-navigation li a {
    color: white;
}

您应该将.item-group-selected选项移到和/或赋予它更高特异性#item-group-navigation

#item-group-navigation li a {
    color: white;
}

#item-group-navigation li.item-group-selected a {
    color:rgb(245, 255, 0);
}
于 2013-06-13T08:35:27.317 回答
1

不应该是:

.item-group-selected{
    color:rgb(245, 255, 0) !important;
}

记下分号的位置。

于 2013-06-13T08:34:18.017 回答
1

它应该是

.item-group-selected{
    color:rgb(245, 255, 0) !important;
}

;应该在之后!important

于 2013-06-13T08:34:38.967 回答