[英]Are there any benefits to setting a value for an HTML boolean attribute?
I know that a boolean attribute can optionally have a value. 我知道一个布尔属性可以有选择地有一个值。 From the HTML5 spec:
根据HTML5规范:
The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
元素上的布尔属性的存在表示真实值,而该属性的缺失表示错误值。 If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
如果存在该属性,则其值必须为空字符串或该属性的规范名称的ASCII大小写不匹配的值,且没有前导或尾随空格。 Note: The values "true" and "false" are not allowed on boolean attributes.
注意:布尔属性上不允许使用值“ true”和“ false”。 To represent a false value, the attribute has to be omitted altogether.
为了表示一个假值,必须完全省略该属性。
I suppose there is a valid historical reason that explains why a value is allowed (and only allow a case-insensitive name match)... but my question is: What are the benefits, if any, of having a value for these boolean attributes? 我想有一个历史原因可以解释为什么允许值(并且只允许不区分大小写的名称匹配)...但是我的问题是:为这些布尔属性取值有什么好处(如果有的话) ? (Or disadvantages, if any.)
(或缺点,如果有的话。)
For example: 例如:
<option selected="selected">...</option>
...
<input required="required" ... />
versus 与
<option selected>...</option>
...
<input required ... />
如果出于某种原因需要执行XHTML,则必须这样做,因为属性必须具有XML值。
Using an empty string is consistent with having the attribute without a value. 使用空字符串与使属性没有值一致。 If you set a boolean attribute to empty string then the attribute will show up as unset (without a value) in Chrome dev tools.
如果您将布尔属性设置为空字符串,则该属性将在Chrome开发者工具中显示为未设置(无值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.