热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

一个css属性值优先于另一个-Priorityofonecssattributevalueoveranother

ForabuttonIhave3possibleclasses:state-normal,state-focusandstate-hover.Allhaveth

For a button I have 3 possible classes: "state-normal", "state-focus" and "state-hover". All have the same attributes (background, border, ...), but different values for the attributes.
If a button gets "state-focus", I do not want to remove the class "state-normal".
If a button is "state-focus" and gets "state-hover", I do not want to remove the class "state-focus".
In the browser language specification you can give a "quality"/priority to a language:

对于一个按钮,我有3个可能的类:“state-normal”,“state-focus”和“state-hover”。所有属性都具有相同的属性(背景,边框,...),但属性的值不同。如果按钮变为“状态焦点”,我不想删除“state-normal”类。如果按钮是“状态焦点”并且获得“状态悬停”,我不想删除类“state-focus”。在浏览器语言规范中,您可以为语言提供“质量”/优先级:

"Accept-Language: da, en-gb;q=0.8, en;q=0.7"

It would be great to do the same also in css:

在css中做同样的事情会很棒:

.state-normal { background-color: #aaaaaa;q=0.5 }
.state-focus  { background-color: #bbbbbb;q=0.7 }
.state-hover  { background-color: #eeeeee;q=0.9 }

I know that there is nothing in CSS.

我知道CSS中没有任何内容。

But, I know in jQuery UI they have kind of this, because they don't remove "ui-state-default" when they assign "ui-state-focus" to an element. How do they do it?

但是,我知道在jQuery UI中他们有这种情况,因为当他们将“ui-state-focus”分配给元素时,他们不会删除“ui-state-default”。他们是如何做到的呢?

Is there another way to implement this with a trick (WITHOUT !IMPORTANT).

有没有另一种方法来实现这个技巧(没有!重要)。

Thanks alot in advance

非常感谢提前

1 个解决方案

#1


3  

You can do this using CSS.

你可以用CSS做到这一点。

.state-normal { background-color: #aaaaaa;q=0.5 }
.state-normal.state-focus  { background-color: #bbbbbb;q=0.7 }
.state-focus.state-hover  { background-color: #eeeeee;q=0.9 }

But this implies that all classes mentioned in the rule will be present, i.e. an element will have both classes present. So an element with class state-focus will not have the background-color set as per the rule.

但这意味着规则中提到的所有类都将存在,即元素将同时存在两个类。因此,具有类state-focus的元素将不会根据规则设置背景颜色。

If you want to avoid that, then you can do this instead:

如果你想避免这种情况,那么你可以这样做:

.state-normal { background-color: #aaaaaa;q=0.5 }
.state-focus, .state-normal.state-focus  { background-color: #bbbbbb;q=0.7 }
.state-hover, .state-focus.state-hover  { background-color: #eeeeee;q=0.9 }

EDIT: As per OP's request

编辑:根据OP的要求

CSS Specificity

CSS Selectors - MDN

CSS选择器 - MDN

Similar answer


推荐阅读
author-avatar
达达2502854565
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有