检查=“检查” vs checked = true [英] checked = "checked" vs checked = true

查看:91
本文介绍了检查=“检查” vs checked = true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两种用法有什么不同?

  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆