简体   繁体   English

为什么这个重要的CSS值被覆盖了?

[英]Why is this !important CSS value overridden?

HTML: HTML:

<div id="test">This is a test</div>

JavaScript: JavaScript的:

var elem = document.getElementById('test');

elem.style.setProperty('color', 'green', 'important');
elem.style.color = 'red';

Live demo: http://jsfiddle.net/4fn6h/3/ 现场演示: http //jsfiddle.net/4fn6h/3/

The text is green in Chrome, Safari, and IE9, but red in Firefox, and Opera. Chrome,Safari和IE9中的文本为绿色,但Firefox和Opera中的文本为红色。 (Also, the text is black in IE7, and IE8, because the code throws an error, but let's ignore that... ) (另外,IE7和IE8中的文本是黑色的,因为代码会抛出错误,但让我们忽略它...)

So, which browsers follow the standard here? 那么,哪些浏览器遵循此标准? Should it be possible to override a setProperty(...,'important') or not? 是否可以覆盖setProperty(...,'important')

The spec is not clear. 规格尚不清楚。 There are two ways to look at it: 有两种方法可以看待它:

  1. it's a bug in WebKit/IE9. 这是WebKit / IE9中的一个错误。 If you are overwriting the color value, there is no reason for the old value to stay around, important or not. 如果要覆盖color值,则没有理由保留旧值, 重要不重要
  2. WebKit/IE9 are correct. WebKit / IE9是正确的。 The DOM interface style manipulates the style property of the element, which is considered a CSS Declaration Block . DOM接口style操纵元素的style属性,该属性被视为CSS声明块 In a CSS block, a property with !important set will always take precedence over ones without. 在CSS块中,具有!important set的属性将始终优先于没有。 By that rule the change to 'red' should have no effect, so it's effectively ignored. 根据该规则,对“红色”的改变应该没有效果,因此它实际上被忽略了。

I believe the latter is the most likely explanation. 我相信后者是最可能的解释。 Consider having a declaration like this: 考虑有这样的声明:

p { color: red !important; }

If you add a second color rule, without !important , it has no effect: 如果你添加第二个color规则,没有!important ,它没有效果:

p {
  color: red !important;
  color: blue;
}
/* the color is still red */

The same applies to manipulating the HTML style attribute directly. 这同样适用于直接操作HTML style属性。

So the behavior in Chrome/Safari/IE9 is consistent with the CSS cascading rules, while FF and Opera are treating DOM style as a simple object without regard for the cascading rules, not as an interface to the CSS declarations. 因此,Chrome / Safari / IE9中的行为与CSS级联规则一致,而FF和Opera将DOM样式视为一个简单的对象,而不考虑级联规则,而不是CSS声明的接口。

http://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/css.html#CSS-CSSStyleDeclaration http://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/css.html#CSS-CSSStyleDeclaration


Fun fact: webkit seems to be doing a string match for important , so this works too: 有趣的事实:webkit似乎正在进行important的字符串匹配,所以这也适用:

elem.style.setProperty('color', 'red', 'this is a really important rule');

And a tip: pick a better color pair next time, you're making it hard for the color blind to help :) 还有一个提示:下次选择一个更好的颜色对,你会让色盲很难帮助:)

It could be that Firefox and Opera's behavior is more appropriate. 可能是Firefox和Opera的行为更合适。

When you issue elem.style.color = 'red'; 当你发出elem.style.color ='red'; you did not turn off the "important" priority on the color, in which case it would make sense to change the color to red. 你没有关闭颜色的“重要”优先级,在这种情况下,将颜色更改为红色是有意义的。 As far as why they choose to do it one way or another, I don't know. 至于为什么他们选择这样或那样做,我不知道。

Should it be possible to override a setProperty(...,'important') or not? 是否可以覆盖setProperty(...,'important')? yes it should. 是的它应该。 but you have to do it with another ele.style.setProperty call. 但你必须使用另一个ele.style.setProperty调用。 take a look at this and you should see red in all modern browsers. 看看这个 ,你应该在所有现代浏览器中看到red

So, which browsers follow the standard here? 那么,哪些浏览器遵循此标准? since green is set with !important , it should not be replaced with red since red is not set with !important . 因为green设置为!important ,所以不应该用red替换,因为red没有设置!important that means chrome, safari and IE9 are following the standard and firefox is NOT. 这意味着chrome,safari和IE9遵循标准,而firefox则不是。

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

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