简体   繁体   English

Chrome“ background:0” document.styleSheets中的cssText错误

[英]Chrome “background: 0” wrong cssText in document.styleSheets

I developed a Chrome extension that applies all CSS styles to the "style" attribute of elements(to use in emails etc.). 我开发了一个Chrome扩展程序,该扩展程序将所有CSS样式应用于元素的“样式”属性(用于电子邮件等)。 I've found a strange behavior in Chrome with this css style: 我发现这种CSS样式在Chrome中有奇怪的行为:

button{
    background: 0;
}

in document.styleSheets when I read cssText of this rule I get: 当我阅读此规则的cssText时,在document.styleSheets中得到:

button { background-position: 0px 50%; background-repeat: initial initial; }

But it's wrong because I do not override background-color or background-image properties. 但这是错误的,因为我没有覆盖background-color或background-image属性。 The strange thing is that the background of button actually is "none" after applying background: 0, issue only with cssText. 奇怪的是,应用背景后,按钮的背景实际上为“ none”:0,仅使用cssText发出。

In Firefox I get: 在Firefox中,我得到:

button { background: none repeat scroll 0px center transparent; }

Which is ok. 没关系

JS Fidle Demo(open in Chrome): JS Fidle演示(在Chrome中打开):

JS Fidle JS·菲德尔

Is it Chrome bug or something else? 是Chrome错误还是其他?

By setting " background: 0; " you are assigning the button element an improper value. 通过设置“ background:0; ”,您为按钮元素分配了不合适的值。 Chrome must interpret that improper value differently than other browsers. Chrome浏览器必须以与其他浏览器不同的方式解释该不当价值。 Change " background: 0; " to " background: none; " and that should do the trick. 将“ background:0; ”更改为“ background:none; ”,这应该可以解决问题。

No, it's not a bug, either the returned value is not wrong. 不,这不是错误,返回的值也没有错。 This is how Chrome tries to fix your invalid CSS. 这就是Chrome尝试修复无效CSS的方式。 Firefox seems to be smarter with this, and IE is totally handless in front of your CSS, it returns button { background: 0px; } Firefox似乎更聪明,并且IE在CSS之前完全没有用,它返回button { background: 0px; } ;。 button { background: 0px; } . button { background: 0px; }

Actually the question is quite trivial, there's no standard for how browsers should handle invalid code. 实际上,这个问题很简单,浏览器如何处理无效代码没有标准。 They can try to fix the code as they wish, or even ignore it. 他们可以尝试根据需要修复代码,甚至可以忽略它。

A fix would be to use a proper value in the rule: 解决方法是在规则中使用适当的值:

button {
    background: none;
}

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

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