Html “span”标签中的多个“style”属性:会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28867083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:37:15  来源:igfitidea点击:

Multiple "style" attributes in a "span" tag: what's supposed to happen?

html

提问by Mark

Consider the following HTML fragment with two styleattributes:

考虑以下具有两个style属性的HTML 片段:

<span style="color:blue" style="font-style:italic">Test</span>

In Opera 12.16 and Chrome 40, it shows up as blue non-italic text, while Internet Explorer 9 shows blue italic text. What, if anything, does the standard say is supposed to show up?

在 Opera 12.16 和 Chrome 40 中,它显示为蓝色非斜体文本,而 Internet Explorer 9 显示为蓝色斜体文本。如果有的话,标准说应该出现什么?

回答by Eeji

Separate your rules with a semi colon in a single declaration:

在单个声明中用分号分隔规则:

<span style="color:blue;font-style:italic">Test</span>

<span style="color:blue;font-style:italic">Test</span>

回答by Eeji

In HTML, SGML and XML, (1)attributes cannot be repeated, and should only be defined in an element once.

在 HTML、SGML 和XML 中(1)属性不能重复,并且只能在一个元素中定义一次

So your example:

所以你的例子:

<span style="color:blue" style="font-style:italic">Test</span>

is non-conformantto the HTML standard, and will result in undefined behaviour, which explains why different browsers are rendering it differently.

不符合标准的HTML标准,并会导致不确定的行为,这可以解释为什么不同的浏览器不同的是它的渲染。



Since there is no defined way to interpret this, browsers can interpret it however they want and merge them, or ignore them as they wish.

由于没有定义的方法来解释这一点,浏览器可以按照他们想要的方式解释它并合并它们,或者按照他们的意愿忽略它们。

(1): Every article I can find states that attributes are "key/value" pairs or "attribute-value" pairs, heavily implying the keys must be unique. The best source I can find states:

(1):我能找到的每篇文章都说明属性是“键/值”对或“属性-值”对,这在很大程度上意味着键必须是唯一的。我能找到的最佳来源状态

Attribute names (id and status in this example) are subject to the same restrictions as other names in XML; they need not be unique across the whole DTD, however, but only within the list of attributes for a given element. (Emphasis mine.)

属性名称(本例中的 id 和 status)与 XML 中的其他名称受到相同的限制;然而,它们不需要在整个 DTD 中是唯一的,而只在给定元素的属性列表中(强调我的。)