html - 空类属性是有效的 HTML 吗?

标签 html

是一个空的 class 属性,具有以下格式的有效 HTML:

<p class="">something</p>
<p class>something</p>

我找到了 this question这很相似,但特别询问自定义数据属性。

最佳答案

在查看其他答案中提到的规范后,我找到了实际回答所提出问题的部分。

<p class>不允许

specification on attributes关于空属性语法的第 3.2.3.1 节说明如下:

An empty attribute is one where the value has been omitted. This is a syntactic shorthand for specifying the attribute with an empty value, and is commonly used for boolean attributes. This syntax may be used in the HTML syntax, but not in the XHTML syntax.

(...)

This syntax is permitted only for boolean attributes.

看到 description of the class attribute (显然)没有提到它是一个 bool 属性,不允许省略该值。

<p class=""> 允许的

来自 the section on class我们了解到:

Every HTML element may have a class attribute specified.

The attribute, if specified, must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.

并且来自 space-seperated tokens 的定义:

A set of space-separated tokens is a string containing zero or more words (known as tokens) separated by one or more space characters, where words consist of any string of one or more characters, none of which are space characters.

我们可以得出结论,属性值实际上可以为空(即包含零个标记)。

关于html - 空类属性是有效的 HTML 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30748847/

相关文章:

php - 如何在按下打开 php 脚本的上传按钮后保留在 .html 上?

javascript - Bootstrap Datepicker 出现在模式中的错误位置

javascript - 如何从 URL 显示图像分辨率

html - Bootstrap 导航栏图标和切换按钮问题

javascript - 在 CSS 网格布局中使用 em 或百分比

javascript - 如何使用JavaScript检测html中是否同时按下多个键

html - 通过 Chrome 中的 iframe 访问时,Youtube 插入的视频不显示全屏按钮

javascript - 以编程方式在 HTML5 SVG 中添加位图图像

jquery - 动画后定位图像

html - 外部 CSS !important 覆盖内联 !important