在 HTML 元素中使用多个单独的类属性是否有效?
像这样:
<div class="first-class" class="second-class">
</div>
Run Code Online (Sandbox Code Playgroud)
我知道可以将多个类名放在一个类属性中,我只是想知道是否允许以另一种方式这样做,还是绝对禁止?
不,您必须只有一个类属性。多个类用空格分隔。
<div class="first-class second-class"></div>
Run Code Online (Sandbox Code Playgroud)
以下是 HTML 标准规范中的参考资料:
同一个开始标签上决不能有两个或多个属性,它们的名称是相互不区分大小写的 ASCII 匹配。
https://html.spec.whatwg.org/multipage/syntax.html#syntax-attributes
如果解析器遇到标记中已经具有同名属性的属性,则会发生此错误。解析器会忽略所有此类重复出现的属性。
https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute