Html readonly="true" 和 readonly="readonly" 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6172911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between readonly="true" & readonly="readonly"?
提问by Ulhas Tuscano
What is the difference between:
有什么区别:
<input name="TextBox1" type="text" id="TextBox1" readonly="true" />
and:
和:
<input name="TextBox1" type="text" id="TextBox1" readonly="readonly" />
When i set readonly
to true
it works somewhat different from readonly='readonly'
. W3C standard says readonly
should be 'readonly'
& not 'true'
. Why most of the browsers allow readonly='true'
which has somewhat different functionality than readonly='readonly'
?
当我设置它时readonly
,true
它的工作方式与readonly='readonly'
. W3C 标准说readonly
should be 'readonly'
& not 'true'
。为什么大多数浏览器都允许readonly='true'
与readonly='readonly'
?
采纳答案by jerone
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.
为元素赋予属性readonly
将赋予该元素只读状态。不管你在它后面放了什么值,或者如果你在它后面放了任何值,它仍然会认为它是只读的。放置readonly="false"
不起作用。
Suggested is to use the W3C standard, which is readonly="readonly"
.
建议使用 W3C 标准,即readonly="readonly"
.
回答by Robert Koritnik
This is a property setting rather than a valued attribute
这是一个属性设置而不是一个有价值的属性
These property settingsare 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
.
这些属性设置是每次看到的值,不需要对它们进行任何分配。当它们存在时,一个元素将此布尔属性设置为true
,当它们不存在时,它们是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.
这当然很好,因为有些框架没有能力添加这些属性而不提供它们的值。Asp.net MVC Html 助手就是其中之一。jQuery 曾经是相同的,直到 1.6 版添加了属性的概念。
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.
当然也有一些与 XHTML 相关的含义,因为 XML 中的属性需要值才能形成良好的格式。但那是另一回事了。因此浏览器必须忽略值分配。
Anyway. Never mind the value you're assigning to themas 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.
这可能会混淆未来的开发人员维护您的代码,并可能干扰未来的规范,这些规范可能会为此类属性设置定义更严格的规则。
回答by KooiInc
readonly="readonly"
is xhtmlsyntax. 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 pagefor more.information.
readonly="readonly"
是xhtml语法。在 xhtml 中布尔属性是这样写的。在 xhtml 中<input type="checkbox" checked>
不允许“属性最小化”( ),因此这是在 xhtml 中包含布尔属性的有效方法。有关更多信息,请参阅此页面。
If your document typeis xhtml transitional or strict and you want to validate it, use readonly="readonly
otherwise readonly
is sufficient.
如果您的文档类型是 xhtml 过渡或严格,并且您想对其进行验证,则使用readonly="readonly
其他类型readonly
就足够了。
回答by Phil
I'm not sure how they're functionally different. My current batch of OS X browsers don't show any difference.
我不确定它们在功能上有何不同。我当前的一批 OS X 浏览器没有任何区别。
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
由于旧版 HTML 属性处理,我认为它们在功能上都是相同的。回到过去,任何标志(布尔)属性只需要存在,没有值,例如
<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.
当 XHTML 出现时,这种语法无效并且需要值。虽然 W3 使用属性名称作为值指定,但我猜大多数浏览器供应商决定简单地检查属性是否存在。
回答by James Jithin
According to HTML standards, the use of
根据 HTML 标准,使用
<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:
足以使输入元素只读。但是,XHTML 标准说上面列出的用法是无效的,因为attribute minimization
. 您可以参考以下链接:
https://www.w3.org/TR/xhtml1/diffs.html#h-4.5
https://www.w3.org/TR/xhtml1/diffs.html#h-4.5