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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:20:44  来源:igfitidea点击:

checked = "checked" vs checked = true

javascripthtml

提问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').checkedis a boolean value. It should be trueor false

document.getElementById('myRadio').checked是一个布尔值。它应该是truefalse

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 truewithout casting.

document.getElementById('myRadio').checked = true;只分配true而不强制转换。

Use trueas it is marginally more efficient and is more intention revealing to maintainers.

使用,true因为它稍微更有效,并且更能向维护者透露意图。

回答by Oded

The original checkedattribute (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 trueor false, so it becomes false.

如果标记中的属性没有值,则该属性变为null,但该属性始终为truefalse,因此变为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 removeAttributeto 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 elemin this answer.

document.getElementById('myRadio')返回 DOM 元素,我将elem在此答案中引用它。

elem.checkedaccesses the propertynamed checkedof 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 truein a boolean context, setting elem.checkedto '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 trueand falsethough so you should stick with the proper values.

但是,由于您拥有 DOM 元素,因此您没有理由设置该属性,因为您可以简单地使用 - 更舒适的 - 布尔属性。由于true在布尔上下文中考虑非空字符串,因此设置elem.checked'checked'或任何其他非虚假值(偶数'false''0')将选中复选框。没有理由不使用truefalse但您应该坚持使用正确的值。

回答by antyrat

checkedattribute is a boolean value so "checked"value of other "string"except boolean falseconverts 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.

所以答案是:它们是平等的。

w3c

w3c