它是自动对焦还是自动对焦

时间:2022-12-06 21:18:30

I seem to recall most (maybe all) attributes in previously versions of HTML (before HTML5) required attributes to have values, like readonly="readonly".

我似乎回想起HTML (HTML5之前)的大多数(可能是所有)属性都需要具有值的属性,比如readonly="readonly"。

Is this true for HTML5 and the autofocus attribute?

这对HTML5和autofocus属性是正确的吗?

3 个解决方案

#1


76  

In HTML, you use boolean attributes with or without values as you like. A boolean, for W3C, like autofocus can be written like that autofocus or autofocus="autofocus" or also autofocus="".

在HTML中,可以根据自己的喜好使用布尔属性或不使用值。对于W3C来说,像autofocus这样的布尔值可以写成autofocus或autofocus="autofocus"或autofocus=""。

If you don't want autofocus just don't write it.

如果你不想要自动对焦,那就不要写。

I think you are confused because XHTML requires values for all attributes: attributes="values".

我认为您很困惑,因为XHTML需要所有属性的值:attributes="values"。

Here is some information about boolean attribute use in HTML: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attribute

这里有一些关于在HTML中使用布尔属性的信息:http://www.whatwg.org/specs/webapps/current - work/multiply age/commonmicrosyntaxes.html #boolean-attribute

#2


34  

Quoting the HTML5 spec and expanding a bit on Pekka:

引用HTML5规范并扩展一点Pekka:

http://www.w3.org/TR/html5/forms.html#autofocusing-a-form-control:-the-autofocus-attribute :

http://www.w3.org/TR/html5/forms.html autofocusing-a-form-control:-the-autofocus-attribute:

The autofocus attribute is a boolean attribute.

autofocus属性是布尔属性。

http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :

http://www.w3.org/TR/html5/infrastructure.html boolean-attributes:

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大小写不敏感的匹配,不带任何引导或尾随空格。

Conclusion:

结论:

The following are valid, equivalent and true:

以下是有效、等价和真实的:

<input type="text" autofocus />
<input type="text" autofocus="" />
<input type="text" autofocus="autofocus" />
<input type="text" autofocus="AuToFoCuS" />

The following are invalid:

以下是无效的:

<input type="text" autofocus="0" />
<input type="text" autofocus="1" />
<input type="text" autofocus="false" />
<input type="text" autofocus="true" />

The absence of the attribute is the only valid syntax for false:

属性的缺失是false的唯一有效语法:

<input type="text"/>

Recommendation

建议

If you care about writing valid XHTML, use autofocus="autofocus", since <input autofocus> is invalid and other alternatives are less readable. Else, just use <input autofocus> as it is shorter.

如果您关心编写有效的XHTML,请使用autofocus="autofocus",因为是无效的,其他替代方法的可读性较差。否则,只需使用,因为它更短。

#3


21  

No, it's enough to specify the attribute itself. It was that way also in HTML 4.

不,指定属性本身就足够了。在HTML 4中也是如此。

A number of attributes are boolean attributes. 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大小写不敏感的匹配,不带任何引导或尾随空格。

Example:

例子:

<label><input type=checkbox checked name=cheese disabled> Cheese</label>

#1


76  

In HTML, you use boolean attributes with or without values as you like. A boolean, for W3C, like autofocus can be written like that autofocus or autofocus="autofocus" or also autofocus="".

在HTML中,可以根据自己的喜好使用布尔属性或不使用值。对于W3C来说,像autofocus这样的布尔值可以写成autofocus或autofocus="autofocus"或autofocus=""。

If you don't want autofocus just don't write it.

如果你不想要自动对焦,那就不要写。

I think you are confused because XHTML requires values for all attributes: attributes="values".

我认为您很困惑,因为XHTML需要所有属性的值:attributes="values"。

Here is some information about boolean attribute use in HTML: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attribute

这里有一些关于在HTML中使用布尔属性的信息:http://www.whatwg.org/specs/webapps/current - work/multiply age/commonmicrosyntaxes.html #boolean-attribute

#2


34  

Quoting the HTML5 spec and expanding a bit on Pekka:

引用HTML5规范并扩展一点Pekka:

http://www.w3.org/TR/html5/forms.html#autofocusing-a-form-control:-the-autofocus-attribute :

http://www.w3.org/TR/html5/forms.html autofocusing-a-form-control:-the-autofocus-attribute:

The autofocus attribute is a boolean attribute.

autofocus属性是布尔属性。

http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :

http://www.w3.org/TR/html5/infrastructure.html boolean-attributes:

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大小写不敏感的匹配,不带任何引导或尾随空格。

Conclusion:

结论:

The following are valid, equivalent and true:

以下是有效、等价和真实的:

<input type="text" autofocus />
<input type="text" autofocus="" />
<input type="text" autofocus="autofocus" />
<input type="text" autofocus="AuToFoCuS" />

The following are invalid:

以下是无效的:

<input type="text" autofocus="0" />
<input type="text" autofocus="1" />
<input type="text" autofocus="false" />
<input type="text" autofocus="true" />

The absence of the attribute is the only valid syntax for false:

属性的缺失是false的唯一有效语法:

<input type="text"/>

Recommendation

建议

If you care about writing valid XHTML, use autofocus="autofocus", since <input autofocus> is invalid and other alternatives are less readable. Else, just use <input autofocus> as it is shorter.

如果您关心编写有效的XHTML,请使用autofocus="autofocus",因为是无效的,其他替代方法的可读性较差。否则,只需使用,因为它更短。

#3


21  

No, it's enough to specify the attribute itself. It was that way also in HTML 4.

不,指定属性本身就足够了。在HTML 4中也是如此。

A number of attributes are boolean attributes. 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大小写不敏感的匹配,不带任何引导或尾随空格。

Example:

例子:

<label><input type=checkbox checked name=cheese disabled> Cheese</label>