What is the difference between readonly=“true” & readonly=“readonly”?

后端 未结 6 972
攒了一身酷
攒了一身酷 2020-11-29 03:04

What is the difference between:


and:

<         


        
相关标签:
6条回答
  • 2020-11-29 03:39

    readonly="true" is invalid HTML5, readonly="readonly" is valid.

    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:

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

    The following are invalid:

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

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

    <input type="text"/>
    

    Recommendation

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

    0 讨论(0)
  • 2020-11-29 03:39

    readonly="readonly" is xhtml syntax. In xhtml boolean attributes are written this way. In xhtml 'attribute minimization' (<input type="checkbox" checked>) isn't allowed, so this is the valid way to include boolean attributes in xhtml. See this page for more.information.

    If your document type is xhtml transitional or strict and you want to validate it, use readonly="readonly otherwise readonly is sufficient.

    0 讨论(0)
  • 2020-11-29 03:53

    This is a property setting rather than a valued attribute

    These property settings are values per see and don't need any assignments to them. When they are present, an element has this boolean property set to true, when they're absent they're false.

    <input type="text" readonly />
    

    It's actually browsers that are liberal toward value assignment to them. If you assign any value to them it will simply get ignored. Browsers will only see the presence of a particular property and ignore the value you're trying to assign to them.

    This is of course good, because some frameworks don't have the ability to add such properties without providing their value along with them. Asp.net MVC Html helpers are one of them. jQuery used to be the same until version 1.6 where they added the concept of properties.

    There are of course some implications that are related to XHTML as well, because attributes in XML need values in order to be well formed. But that's a different story. Hence browsers have to ignore value assignments.

    Anyway. Never mind the value you're assigning to them as long as the name is correctly spelled so it will be detected by browsers. But for readability and maintainability it's better to assign meaningful values to them like:

    readonly="true" <-- arguably best human readable
    readonly="readonly"
    

    as opposed to

    readonly="johndoe"
    readonly="01/01/2000"
    

    that may confuse future developers maintaining your code and may interfere with future specification that may define more strict rules to such property settings.

    0 讨论(0)
  • 2020-11-29 04:01

    Giving an element the attribute readonly will give that element the readonly status. It doesn't matter what value you put after it or if you put any value after it, it will still see it as readonly. Putting readonly="false" won't work.

    Suggested is to use the W3C standard, which is readonly="readonly".

    0 讨论(0)
  • 2020-11-29 04:02

    I'm not sure how they're functionally different. My current batch of OS X browsers don't show any difference.

    I would assume they are all functionally the same due to legacy HTML attribute handling. Back in the day, any flag (Boolean) attribute need only be present, sans value, eg

    <input readonly>
    <option selected>
    

    When XHTML came along, this syntax wasn't valid and values were required. Whilst the W3 specified using the attribute name as the value, I'm guessing most browser vendors decided to simply check for attribute existence.

    0 讨论(0)
  • 2020-11-29 04:03

    According to HTML standards, the use of

    <input name="TextBox1" type="text" id="TextBox1" readonly/>
    

    is enough to make the input element readonly. But, XHTML standard says that the usage listed above is invalid due to attribute minimization. You can refer to the links below:

    https://www.w3.org/TR/xhtml1/diffs.html#h-4.5

    http://www.w3schools.com/tags/att_input_readonly.asp

    0 讨论(0)
提交回复
热议问题