CSS 背景颜色优先级
我正在使用 jQuery 更改悬停时表格行的颜色,我注意到在以下实例中我得到了不同的结果。
CSS 如下:
.normalcolor {
background-color: #dedede;
}
.reddishcolor {
background-color: #ffc59c;
}
.hovercolor {
background-color: #f1e1c0;
}
现在,我正在使用 jQuery 执行悬停效果,使用以下代码:
$("table.withhover tr").hover(function(){
$(this).addClass("hovercolor");
}, function(){
$(this).removeClass("hovercolor");
});
奇怪的是,当我将鼠标悬停在带有 class="normalcolor"
的行上时,背景颜色更改为 .hovercolor
中的颜色。但是,当我将鼠标悬停在 class="reddishcolor"
行上时,背景颜色不会改变。
这是正常的吗?如果是,为什么?跟颜色有关系吗?
谢谢!
I'm changing the colors of a table rows on hover with jQuery and I've noticed that in the following instances I get different results.
The CSS is as follows:
.normalcolor {
background-color: #dedede;
}
.reddishcolor {
background-color: #ffc59c;
}
.hovercolor {
background-color: #f1e1c0;
}
Now, I'm doing the hover effect with jQuery, using this code:
$("table.withhover tr").hover(function(){
$(this).addClass("hovercolor");
}, function(){
$(this).removeClass("hovercolor");
});
The strange thing is that when I hover over a row with class="normalcolor"
, the background color changes to the one from .hovercolor
. However, when I hover a row with class="reddishcolor"
the background color doesn't change.
Is this normal? And if yes, why? It has anything to do with the colors?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否完全按照该顺序指定了 CSS 类,还是最后指定了
.reddishcolor
类?当多个样式应用于一个元素时,将使用最具体的规则。如果规则同样具体(如您的情况),则使用最新规则。
如果您无法更改 CSS 规则的顺序,则可以使悬停类更加具体,例如还指定元素名称:
Have you specified the CSS classes in exactly that order, or is the
.reddishcolor
class specificed last?When more than one style applies to an element, the most specific rule is used. If the rules are equally specific (as in your case), the latest rule is used.
If you can't change the order of the CSS rules, you can make the hover class more specific, for example by specifying the element name also:
您必须删除定义默认颜色的旧 CSS 类,然后添加定义悬停颜色的新类:
...或者您可以使用
!important
CSS 关键字,使您的悬停颜色类优先于附加到元素的其他类:注意:IE6 和 FireFox 1.X 不支持
!important
关键字。...或者您可以更具体地确定悬停颜色类别的范围:
You have to remove the old CSS class which defines the default color, then add the new class which defines the hover color:
... Or you could play around with the
!important
CSS keyword, to make your hover color class take precedence over other classes attached to an element:Note: The
!important
keyword is not supported by IE6, and FireFox 1.X.... Or you could be more specific in your scoping of your hover color class: