问题描述
以下两种用法有什么不同?
document.getElementById('myRadio')。checked =checked ;
和
document.getElementById('myRadio')。checked = true;
对我而言,两者的行为方式都是一样的。但是,我只是想知道为什么存在两种方法来做同样的事情。
哪一个会是最理想的用法?我需要支持IE7和更高版本。
document.getElementById('myRadio')。checked
是一个布尔值。它应该是 true
或 false
document.getElementById('myRadio')。checked =checked;
将字符串强制转换为布尔值,这是正确的。
document.getElementById('myRadio')。checked = true;
只是在不投射的情况下分配 true
。
使用 true
,因为它稍微更有效率,并且对维护者更有意义。
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.
document.getElementById('myRadio').checked
is a boolean value. It should be true
or false
document.getElementById('myRadio').checked = "checked";
casts the string to a boolean, which is true.
document.getElementById('myRadio').checked = true;
just assigns true
without casting.
Use true
as it is marginally more efficient and is more intention revealing to maintainers.
这篇关于检查=“检查” vs checked = true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!