输入文本元素的正确只读属性语法是什么?

2023-12-24

和大多数人一样,我熟悉readonly属性为text input,但是在阅读其他网站的代码时(我的一个讨厌的习惯),我看到了该属性的多个实现:

<input type="text" value="myvalue" class="class anotherclass" readonly >

and

<input type="text" value="myvalue" class="class anotherclass" readonly="readonly" >

我什至见过

<input type="text" value="myvalue" class="class anotherclass" readonly="true" >

..我相信我看到了更多,但现在记不起确切的语法了..

那么,我应该使用哪一种是正确的呢?


HTML5 规范:

http://www.w3.org/TR/html5/forms.html#attr-input-readonly http://www.w3.org/TR/html5/forms.html#attr-input-readonly :

只读属性是一个布尔属性

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

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

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

结论:

以下是有效、等效且真实:

<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />

以下是invalid:

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

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

<input type="text"/>

推荐

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

输入文本元素的正确只读属性语法是什么? 的相关文章

随机推荐