Obm*_*nen 62 html forms readonly input-field
我知道它的readonly属性text input,但是当从其他站点读取代码时(我的一个讨厌的习惯)我看到了不止一个实现:
<input type="text" value="myvalue" class="class anotherclass" readonly >
Run Code Online (Sandbox Code Playgroud)
和
<input type="text" value="myvalue" class="class anotherclass" readonly="readonly" >
Run Code Online (Sandbox Code Playgroud)
我甚至见过
<input type="text" value="myvalue" class="class anotherclass" readonly="true" >
Run Code Online (Sandbox Code Playgroud)
..而且我相信我看到了更多,但现在不记得确切的语法..
那么,哪一个是我应该使用的正确的呢?
Cir*_*四事件 70
HTML5规范:
http://www.w3.org/TR/html5/forms.html#attr-input-readonly:
readonly属性是一个布尔属性
http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes:
元素上存在布尔属性表示真值,缺少属性表示false值.
如果该属性存在,则其值必须是空字符串,或者是属性的规范名称的ASCII不区分大小写匹配的值,没有前导或尾随空格.
结论:
以下是有效,等效和真实的:
<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />
Run Code Online (Sandbox Code Playgroud)
以下是无效的:
<input type="text" readonly="0" />
<input type="text" readonly="1" />
<input type="text" readonly="false" />
<input type="text" readonly="true" />
Run Code Online (Sandbox Code Playgroud)
缺少该属性是false的唯一有效语法:
<input type="text"/>
Run Code Online (Sandbox Code Playgroud)
建议
如果您关心编写有效的XHTML,请使用readonly="readonly",因为它<input readonly>是无效的,而其他替代方案的可读性较差.否则,只需使用<input readonly>它,因为它更短.