分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

属性只有一个值的这类 html 属性是怎么回事,该如何设置值;比如:checked = “checked” vs checked = true

发布时间:2023-09-06 01:37责任编辑:郭大石关键词:暂无标签

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

问:

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

答:

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 就对了。

属性只有一个值的这类 html 属性是怎么回事,该如何设置值;比如:checked = “checked” vs checked = true

原文地址:https://www.cnblogs.com/ShawnYang/p/8315255.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved