What is the correct readonly attribute syntax for input text elements?

后端 未结 3 1242
借酒劲吻你
借酒劲吻你 2020-11-30 09:29

As Most, I am familiar with the readonly attribute for text input, But while reading code from other websites (a nasty habit of mine ) I saw more t

3条回答
  •  时光取名叫无心
    2020-11-30 10:01

    HTML5 spec:

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

    The readonly attribute is a boolean attribute

    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.

    Conclusion:

    The following are valid, equivalent and true:

    
    
    
    
    

    The following are invalid:

    
    
    
    
    

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

    
    

    Recommendation

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

提交回复
热议问题