简体   繁体   English

为HTML布尔属性设置值是否有好处?

[英]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.

相关问题 将css属性的值设置为html属性的值 - Setting the value of css property to value of a html attribute 将id属性的值设置为html中的变量 - setting value of id attribute as variable in html 在html中使用包含空格的变量设置“值”属性 - setting a “value” attribute in html with a variable containing spaces 在HTML代码中使用大量的换行符有什么好处? - Are there any benefits to having huge linebreaks in HTML code? 获取html数据属性字符串,即使其值为布尔值 - Getting html data attribute string even its value boolean 将 html 输入的选中属性绑定到 blazor 中的 boolean 方法返回值 - Binding the checked attribute of html input to boolean method return value in blazor html中的“ value”属性为空和根本没有value属性之间有什么区别吗? - Is there any difference between “value” attribute to be empty and no value attribute at all in html? 在客户端动态设置 HTML lang 属性有什么缺点吗? - Are there any drawbacks when setting the HTML lang attribute dynamically on the client side? 通过击倒js将HTML标签属性设置为包含HTML实体的值 - Setting HTML tag attribute with value containing HTML entity, via knockoutjs 使用JQuery设置名为“required”的属性和任何值都不起作用 - Setting an attribute named “required” and any value, with JQuery, doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM