Html 输入文本元素的正确只读属性语法是什么?
与大多数人一样,我熟悉Html 输入文本元素的正确只读属性语法是什么?,html,forms,readonly,input-field,Html,Forms,Readonly,Input Field,与大多数人一样,我熟悉文本输入的只读属性,但在阅读其他网站的代码时(我的一个坏习惯),我看到了该属性的不止一个实现: <input type="text" value="myvalue" class="class anotherclass" readonly > 及 我甚至看到 <input type="text" value="myvalue" class="class anotherclass" readonly="true" > 。。我相信我看到了更
文本输入的只读
属性,但在阅读其他网站的代码时(我的一个坏习惯),我看到了该属性的不止一个实现:
<input type="text" value="myvalue" class="class anotherclass" readonly >
及
我甚至看到
<input type="text" value="myvalue" class="class anotherclass" readonly="true" >
。。我相信我看到了更多,但现在想不起确切的语法
那么,我应该使用哪一个呢?来源:
只读=“只读”或“”(空字符串)或空-
指定元素表示其值不可编辑的控件
所以基本上是一样的。应该是
<input type="text" value="myvalue" class="class anotherclass" readonly="readonly" />
HTML5规范:
<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />
<input type="text" readonly="0" />
<input type="text" readonly="1" />
<input type="text" readonly="false" />
<input type="text" readonly="true" />
<input type="text"/>
:
只读属性是一个布尔属性
:
元素上存在布尔属性表示真值,不存在该属性表示假值
如果属性存在,则其值必须是空字符串或与属性规范名称不区分大小写的ASCII匹配值,且不带前导或尾随空格
结论:
<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />
<input type="text" readonly="0" />
<input type="text" readonly="1" />
<input type="text" readonly="false" />
<input type="text" readonly="true" />
<input type="text"/>
以下内容有效、等效且真实:
<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />
<input type="text" readonly="0" />
<input type="text" readonly="1" />
<input type="text" readonly="false" />
<input type="text" readonly="true" />
<input type="text"/>
以下内容无效:
<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />
<input type="text" readonly="0" />
<input type="text" readonly="1" />
<input type="text" readonly="false" />
<input type="text" readonly="true" />
<input type="text"/>
缺少属性是false的唯一有效语法:
<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />
<input type="text" readonly="0" />
<input type="text" readonly="1" />
<input type="text" readonly="false" />
<input type="text" readonly="true" />
<input type="text"/>
建议
如果您关心如何编写有效的XHTML,请使用readonly=“readonly”
,因为
无效,而其他替代方法的可读性较差。否则,只需使用较短的
。即使它确实有效,也不意味着您应该使用它。我的建议是坚持标准。不要因为可以就引入错误。@MrLister标准建议坚持该标准,即使它是循环的,也会带来更大的影响:)可能重复stackoverflow.com/questions/1033944/…因为两者都是布尔属性(未标记),密切相关,但重点是实现状态,而不是标准:感谢您指出规范是一个绝对的大脑屁:根据设计,您无法计算属性值来切换它(例如,服务器上的模板HTML-PHP/JSP/ASP-或客户端-Angular-属性值为true/false)。必须删除该属性才能将其禁用。委员会里没有人意识到这是一个多么糟糕的事情???@Roboprog哈哈,没错,这有点烦人。解释得很好。这应该是公认的答案。这只是三种有效形式中的一种。西罗·桑蒂利的答案要好得多。