【问题标题】:is it autofocus="autofocus" or autofocus?是自动对焦=“自动对焦”还是自动对焦?
【发布时间】:2010-12-15 00:50:09
【问题描述】:

我似乎记得以前版本的 HTML(HTML5 之前)中的大多数(可能是所有)属性都需要属性具有值,例如 readonly="readonly"

对于 HTML5 和 autofocus 属性是否如此?

【问题讨论】:

  • stackoverflow.com/questions/1033944/… 的可能重复项,因为两者都是关于布尔属性(未标记)
  • 我同意它们相似,但略有不同,因为它们询问的是不同的属性。
  • 自动对焦仅适用。在 FF、Opera 和 Chrome 中对其进行了测试..

标签: html autofocus


【解决方案1】:

在 HTML 中,您可以根据需要使用带有或不带有值的布尔属性。 W3C 的布尔值,如自动对焦,可以写成 autofocusautofocus="autofocus"autofocus=""

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

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

以下是有关在 HTML 中使用布尔属性的一些信息: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attribute

【讨论】:

  • +1 用于提及 XHTML。 XHTML 的 XML 一致性是出现 disabled="disabled" 的唯一原因。关闭标签也是如此。在 HTML 中,并非每个标签都需要关闭(例如 br 或 input),但由于 XHTML 必须是有效的 XML,因此您也需要关闭标签。
  • “XHTML 是出现disabled="disabled" 的唯一原因”。而 SGML 是 XHTML 具有 silly="silly" bool 语法而不仅仅是 whatever="true" 的唯一原因,而 HTML 是 this="false" 可以表示 this = true 的唯一原因。 :)
【解决方案2】:

引用 HTML5 规范 并稍微扩展一下 Pekka:

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

自动对焦属性是一个布尔属性。

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

元素上存在布尔属性表示真值,不存在该属性表示假值。

如果该属性存在,则其值必须是空字符串或与属性的规范名称匹配的不区分大小写的 ASCII 值,并且没有前导或尾随空格。

结论

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

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

以下是无效

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

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

<input type="text"/>

推荐

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

【讨论】:

  • “如果你关心... XHTML”。 不。让它死吧。
【解决方案3】:

No,指定属性本身就足够了。 HTML 4也是这样。

许多属性是布尔属性。元素上存在布尔属性表示真值,不存在该属性表示假值。

如果该属性存在,则其值必须是空字符串或与属性的规范名称匹配的不区分大小写的 ASCII 值,并且没有前导或尾随空格。

例子:

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

【讨论】:

  • 给蒂姆接受的答案......他比你更需要代表:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
相关资源
最近更新 更多