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

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

What is the difference between:


and:

<         


        
6条回答
  •  伪装坚强ぢ
    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.

    
    

    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.

提交回复
热议问题