html怎么设置只有一个,属性只有一个值的这类 html 属性是怎么回事,该如何设置值;比如-站长资讯中心...

这篇博客讨论了在JavaScript中设置HTML元素,特别是单选按钮(radio)选中状态的两种方法:`checked='checked'`和`checked=true`。尽管两者在实际效果上表现相同,但推荐使用`checked=true`,因为它更直接地赋值布尔值,提高效率,并且对于代码的可读性更好,尤其对于维护者而言。
摘要由CSDN通过智能技术生成

c4468b3f4df77e96b0a416fa2a870fba.png

参考链接:https://stackoverflow.com/questions/10650233/checked-checked-vs-checked-true

问:

e4ebb6fcd0bd1257aa600c762fe427d4.png

2909d1e7bbd9aa039caa961e632ec34f.gif

33542c8955472b83c9fadc01d30d8ff8.gif

1 What is the difference between the below two usages?2

3 document.getElementById('myRadio').checked = "checked";4 and5

6 document.getElementById('myRadio').checked = true;7 For me, both are behaving the same way. But, I am just curious to know why there exist two methods to do the same.8

9 Which one will be the ideal usage? I need to support IE7 and higher versions.

View Code

答:

a515158dd3e497d420764015efe508d8.png

2909d1e7bbd9aa039caa961e632ec34f.gif

33542c8955472b83c9fadc01d30d8ff8.gif

1 document.getElementById('myRadio').checked is a boolean value. It should be true or false2

3 document.getElementById('myRadio').checked = "checked"; casts the string to a boolean, which is true.4

5 document.getElementById('myRadio').checked = true; just assigns true without casting.6

7 Use true as it is marginally more efficient and is more intention revealing to maintainers.

View Code

document.getElementById('myRadio')。checked是一个布尔值。 这应该是真的或假的

document.getElementById('myRadio')。checked =“checked”; 将字符串转换为布尔值,这是正确的。

document.getElementById('myRadio')。checked = true; 只是分配true而不投射。

使用true,因为它的效率稍微高一点,而且更有意向维护者展示。

所以设置这种单属性的值为 true 或 false 就对了。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值