Javascript 已检查 = “已检查” vs 已检查 = 真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10650233/
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
checked = "checked" vs checked = true
提问by hop
What is the difference between the below two usages?
以下两种用法有什么区别?
document.getElementById('myRadio').checked = "checked";
and
和
document.getElementById('myRadio').checked = true;
For me, both are behaving the same way. But, I am just curious to know why there exist two methods to do the same.
对我来说,两者的行为方式都是一样的。但是,我只是想知道为什么有两种方法可以做到这一点。
Which one will be the ideal usage? I need to support IE7 and higher versions.
哪一种将是理想的用法?我需要支持IE7及更高版本。
回答by Quentin
document.getElementById('myRadio').checked
is a boolean value. It should be true
or false
document.getElementById('myRadio').checked
是一个布尔值。它应该是true
或false
document.getElementById('myRadio').checked = "checked";
casts the string to a boolean, which is true.
document.getElementById('myRadio').checked = "checked";
将字符串转换为布尔值,这是真的。
document.getElementById('myRadio').checked = true;
just assigns true
without casting.
document.getElementById('myRadio').checked = true;
只分配true
而不强制转换。
Use true
as it is marginally more efficient and is more intention revealing to maintainers.
使用,true
因为它稍微更有效,并且更能向维护者透露意图。
回答by Oded
The original checked
attribute (HTML 4 and before) did not require a value on it - if it existed, the element was "checked", if not, it wasn't.
原始checked
属性(HTML 4 及之前)不需要值——如果它存在,则该元素被“检查”,如果不存在,则它不是。
This, however is not valid for XHTML that followed HTML 4.
但是,这对于遵循 HTML 4 的 XHTML 无效。
The standard proposed to use checked="checked"
as a condition for true - so both ways you posted end up doing the same thing.
该标准建议checked="checked"
用作 true 的条件 - 因此您发布的两种方式最终都会做同样的事情。
It really doesn't matter which one you use - use the one that makes most sense to you and stick to it (or agree with your team which way to go).
您使用哪一种实际上并不重要 - 使用对您最有意义的一种并坚持下去(或与您的团队达成一致)。
回答by Guffa
The element has both an attribute and a property named checked
. The property determines the current state.
该元素同时具有一个属性和一个名为 的属性checked
。该属性决定了当前状态。
The attribute is a string, and the property is a boolean. When the element is created from the HTML code, the attribute is set from the markup, and the property is set depending on the value of the attribute.
属性是字符串,属性是布尔值。当元素从 HTML 代码创建时,属性是从标记设置的,属性是根据属性的值设置的。
If there is no value for the attribute in the markup, the attribute becomes null
, but the property is always either true
or false
, so it becomes false
.
如果标记中的属性没有值,则该属性变为null
,但该属性始终为true
或false
,因此变为false
。
When you set the property, you should use a boolean value:
设置属性时,应使用布尔值:
document.getElementById('myRadio').checked = true;
If you set the attribute, you use a string:
如果设置属性,则使用字符串:
document.getElementById('myRadio').setAttribute('checked', 'checked');
Note that setting the attribute also changes the property, but setting the property doesn't change the attribute.
请注意,设置属性也会更改属性,但设置属性不会更改属性。
Note also that whatever value you set the attribute to, the property becomes true
. Even if you use an empty string or null
, setting the attribute means that it's checked. Use removeAttribute
to uncheck the element using the attribute:
另请注意,无论您将属性设置为什么值,该属性都会变为true
。即使您使用空字符串 or null
,设置该属性也意味着它已被选中。用于removeAttribute
使用属性取消选中元素:
document.getElementById('myRadio').removeAttribute('checked');
回答by ThiefMaster
document.getElementById('myRadio')
returns you the DOM element, i'll reference it as elem
in this answer.
document.getElementById('myRadio')
返回 DOM 元素,我将elem
在此答案中引用它。
elem.checked
accesses the propertynamed checked
of the DOM element. This property is always a boolean.
elem.checked
访问以DOM 元素命名的属性checked
。此属性始终是布尔值。
When writing HTML you use checked="checked"
in XHTML; in HTML you can simply use checked
. When setting the attribute (this is done via .setAttribute('checked', 'checked')
) you need to provide a value since some browsers consider an empty value being non-existent.
编写 HTML 时,您checked="checked"
在 XHTML 中使用;在 HTML 中,您可以简单地使用checked
. 设置属性时(通过 完成.setAttribute('checked', 'checked')
),您需要提供一个值,因为某些浏览器认为不存在空值。
However, since you have the DOM element you have no reason to set the attribute since you can simply use the - much more comfortable - boolean property for it. Since non-empty strings are considered true
in a boolean context, setting elem.checked
to 'checked'
or anything else that is not a falsy value (even 'false'
or '0'
) will check the checkbox. There is not reason not to use true
and false
though so you should stick with the proper values.
但是,由于您拥有 DOM 元素,因此您没有理由设置该属性,因为您可以简单地使用 - 更舒适的 - 布尔属性。由于true
在布尔上下文中考虑非空字符串,因此设置elem.checked
为'checked'
或任何其他非虚假值(偶数'false'
或'0'
)将选中复选框。没有理由不使用true
,false
但您应该坚持使用正确的值。
回答by antyrat
checked
attribute is a boolean value so "checked"
value of other "string"
except boolean false
converts to true
.
checked
属性是一个布尔值,所以除了布尔"checked"
值"string"
之外的其他值false
转换为true
.
Any string value will be true. Also presence of attribute make it true:
任何字符串值都为真。属性的存在也使它成为真的:
<input type="checkbox" checked>
You can make it uncheked only making boolean change in DOM using JS.
您可以仅在使用 JS 的 DOM 中进行布尔更改来取消选中它。
So the answer is: they are equal.
所以答案是:它们是平等的。