对于哪些html5属性是“空属性语法”,允许? [英] For which html5 attributes is the "empty attribute syntax" allowed?
问题描述
http://www.w3.org/ TR / html-markup / syntax.html#syntax-attr-empty 表示
空属性语法
某些属性可以通过仅提供属性名称来指定,没有值[...]完全等同于将空字符串指定为属性的值。
Empty attribute syntax
Certain attributes may be specified by providing just the attribute name, with no value [… which] is exactly equivalent to specifying the empty string as the value for the attribute.
IIRC,布尔属性经常使用它。但是,某些属性是指什么? 允许此语法在哪些属性中使用哪种元素?或者:不允许在哪里?
IIRC, boolean attributes often use this. However, what does "certain attributes" refer to? In which attributes, on which elements, is this syntax allowed? Or: where is it not allowed?
我找不到完整的列表任何地方。
I could not find a complete list anywhere.
推荐答案
空属性语法只是具有名称和空字符串值的属性的简写,并且没有这本身就意味着什么特别的东西。因此,除了布尔属性之外,任何允许空值的属性,包括 class
,都可以使用空属性语法和任何不允许空值的属性来指定,例如如 id
和类型
,不能使用空属性语法指定。
The empty attribute syntax is just a shorthand for an attribute with a name and an empty-string value, and doesn't mean anything special on its own. So, other than boolean attributes, any attribute that permits empty values, including class
, can be specified with empty attribute syntax, and any attribute that doesn't permit empty values, such as id
and type
, cannot be specified with empty attribute syntax.
您可以使用 Validator.nu 比较以下代码段的验证结果来确认这一点。
You can confirm this by comparing the validation results of the following snippets using Validator.nu.
以下两个片段都应该验证:
Both of the following snippets should validate:
<!DOCTYPE html><title>Test</title>
<body class>
<!DOCTYPE html><title>Test</title>
<body class="">
以下两个代码段都应产生相同的验证错误:
And both of the following snippets should produce the same validation error:
<!DOCTYPE html><title>Test</title>
<body id>
<!DOCTYPE html><title>Test</title>
<body id="">
错误:属性
id <的值不正确元素
body
上的/ code>:ID不能是空字符串。
Error: Bad value for attribute
id
on elementbody
: An ID must not be the empty string.
这篇关于对于哪些html5属性是“空属性语法”,允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!