简体   繁体   English

如何从悬停规则覆盖和重置背景颜色属性?

[英]How to override and reset a background-color property from a hover rule?

Is there a way to reset a background-color property of a :hover rule? 有没有办法重置:hover规则的background-color属性?

I have a list of elements which are highlighted when mouse goes over. 我有一个鼠标悬停时突出显示的元素列表。 I want to apply an additional CSS rule that will disable highlighting. 我想应用一条附加的CSS规则,该规则将禁用突出显示。 Here is a demo: 这是一个演示:

http://jsfiddle.net/vqLuU/1/ http://jsfiddle.net/vqLuU/1/

What should I put in the second appearance of the "style1:hover" rule in order to disable highlighting at all? 为了完全禁用突出显示,我应该在“ style1:hover”规则的第二次显示中添加什么? The result must be the same with case when all "style1:hover" rules are removed. 当删除所有“ style1:hover”规则时,结果必须相同。

I do not want to redefine all styles ("green" and "blue") again. 我不想重新定义所有样式(“绿色”和“蓝色”)。 My goal is to disable the "style1:hover" rule. 我的目标是禁用“ style1:hover”规则。

HTML: HTML:

<div class="style1 green">AAA</div>
<div class="style1 blue">BBB</div>

CSS: CSS:

.green {
    background-color: green;
}
.blue {
    background-color: blue;
}
.style1:hover {
    background-color: red;
}

.style1:hover {
    /* How to disable highlighting from here? */

}

Thanks! 谢谢!

Since the first appearance of the .style1:hover rule cannot be changed or removed, the only way to achieve that is by adding the following rules: 由于无法更改或删除.style1:hover规则的首次出现,因此,实现此目标的唯一方法是添加以下规则:

.green:hover {
    background-color: green;
}
.blue:hover {
    background-color: blue;
}

I feel the need to add a disclaimer: this solution is not very elegant, and I don't think of it as the best solution, I think it's the only possible solution given the requirements. 我觉得需要添加一个免责声明:这个解决方案不是很优雅,我不认为它是最好的解决方案,我认为这是满足要求的唯一可行解决方案。

.style1:hover {
    background-color: red;
}

.style1:hover {
    background-color: transparent !important;
}

Note: this may not be a viable solution as you have not mentioned whether you can use JavaScript. 注意:这可能不是一个可行的解决方案,因为您没有提到是否可以使用JavaScript。

You can remove the CSS rule by editing the stylesheets with JavaScript. 您可以通过使用JavaScript 编辑样式表来删除CSS规则。 However it doesn't feel right to me, so I can't fully recommend this =) Maybe other SOers can comment on this method (see jsFiddle ). 但是对我来说感觉不对,所以我不能完全推荐这个=)也许其他SOers可以评论这个方法(参见jsFiddle )。

for (var i = 0; i < document.styleSheets.length; i++ ) {
    var done = false;
    var sheet = document.styleSheets[i];
    # Some browsers use rules (Chrome) others use cssRules (Firefox)
    var rules = sheet.rules || sheet.cssRules;

    for (var j = 0; j < rules.length; j++) {
        var rule = rules[j];
        var selectorText = rule.selectorText;

        if (selectorText.indexOf(".style1:hover") != -1) {
            sheet.deleteRule(j);
            done = true;
            break;
        }
    }

    if (done) break;
}

Easiest way I can think to achieve what I think you want is to add an extra class to the elements. 我能想到实现我想要的最简单的方法是为元素添加一个额外的类。 I've chosen 'hoverEnabled' 我选择了“ hoverEnabled”

You can simply add or remove the class hoverEnabled to an element to have a different :hover style attached. 您可以简单地将类hoverEnabled添加或删除到元素,以使其具有不同的:hover样式。

CSS CSS

.green {
    background-color: green;
}
.blue {
    background-color: blue;
}
.style1.hoverEnabled:hover {
    background-color: #FFAAAA;
}

.style2.hoverEnabled:hover {
    background-color: #AAAAAA;
}

HTML HTML

<div class="style1 green">AAA</div>
<div class="style1 blue">BBB</div>
<div class="style1 hoverEnabled green">AAA</div>
<div class="style2 hoverEnabled blue">BBB</div>

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

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