为什么不是第二个类属性重写前一个。 HTML [英] Why not the second class attribute overwrites the previous one. HTML

查看:89
本文介绍了为什么不是第二个类属性重写前一个。 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览器解释HTML并在解释结果时显示结果。因此,为什么第一个类属性被第二个属性覆盖。

Browsers interpret the HTML and show the results as it interprets it. So why not the first class attribute gets overwritten by the second.

<span class ='first' class= 'second' >text</span>

但如果我这样做

<span class ='first' class= 'second' style='color: green'>text</span>

所有先前的样式都会被覆盖。

all previous styling is overwritten.

这是小提琴

谢谢。

推荐答案

两次使用相同的属性是无效的HTML,并且浏览器执行错误恢复。

Having the same attribute twice is invalid HTML, and browsers perform error recovery.

具体来说,从规范< a>:

Specifically, from the specification:


当用户代理离开属性名称状态(并在发出标记标记之前,如果合适),完整属性的名称必须与同一标记上的其他属性进行比较;如果令牌上已经有一个具有完全相同名称的属性,那么这是一个解析错误,并且必须删除新属性以及与其关联的值(如果有)。

When the user agent leaves the attribute name state (and before emitting the tag token, if appropriate), the complete attribute's name must be compared to the other attributes on the same token; if there is already an attribute on the token with the exact same name, then this is a parse error and the new attribute must be dropped, along with the value that gets associated with it (if any).

因此,第二个class属性被忽略,因为开始标签已经有一个类属性。

So the second class attribute is ignored because the start tag already has a class attribute.

您可以在元素上指定多个类,因为该属性接受空格分隔列表: class =first second

(Note that you can specify multiple classes on an element since the attribute takes a space separated list: class="first second")

样式属性不覆盖类属性。元素将具有与其相关联的两个属性。但是,当应用样式表时,样式属性规则将具有较高的特异性比具有类选择器的规则,因此在样式属性中指定的属性可以赢得级联。

Style attributes do not overwrite class attributes. The element will have both attributes associated with it. However, when the style sheet is applied, a style attribute rule will have a higher specificity than a rule with a class selector, so properties specified in style attributes win the cascade.

这篇关于为什么不是第二个类属性重写前一个。 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆