我正在使用 jquery 将 true 和 false 字符串写入 data-html 属性。但是,当我将 false 写入其中一个属性,然后检查它是否为 false 时,它会返回 true。我还在网上读到它应该这样做,这真的毫无意义。
有人有替代方案吗?任何帮助将非常感激!
(function() {
// $(".opt-header").click(function () {
$(document).on("click", ".opt-header", function() {
console.log($(this).attr("data-is-panel-activated"));
var is_activate = Boolean($(this).attr("data-is-panel-activated"));
$(this).siblings(":not(.opt-group-toggle)").slideToggle(300);
console.log(is_activate);
if (is_activate) {
$(this).find(".fa-angle-right").replaceWith("<i class='fa fa-angle-down'></i>");
$(this).attr("data-is-panel-activated", "false");
} else {
$(this).attr("data-is-panel-activated", "true");
$(this).find(".fa-angle-down").replaceWith("<i class='fa fa-angle-right'></i>");
}
})
})();
Boolean('false')
将返回true
因为字符串'false'
是真的。
您可以运行检查来查看字符串是否等于“false”:
if ($(this).attr("data-is-panel-activated") === 'false') {
.....
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)