设置
因此,在我的程序的一个区域中,我将自定义 html 属性设置为 true:
$(document).on('click', '.profile-player-show', function(event) {
$('.player-profile-pop').attr('inLineup', true);
}
然后,当在另一个稍后的函数中检索该信息时,我注意到 bool 值现在具有 String 类型,这并不令人震惊,我只是想知道规则是什么......?这是我在后者中的代码示例:
var inLineup = $('.player-profile-pop').attr('inLineup');
console.log("In Lineup:" + inLineup + " Type of data:" + typeof inLineup);
var btnText = (inLineup === 'true') ? "Remove From Lineup" : "Add To Lineup";
调查结果
控制台日志:
In Lineup:true Type of data:string
- html 属性中只允许使用字符串
我的问题:
那么,使用 jQuery .attr() 函数设置 true 或 false 等变量时, bool 值是否会自动转换为字符串?
最佳答案
是的。属性值只能是字符串。
关于javascript - 使用 jQuery .attr() 时 bool 值是否会自动转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33525998/