jQuery cookie if语句布尔难题
让下面的if语句检查cookie的存在以及它是真是假。由于某种原因,它不起作用 代码如下:jQuery cookie if语句布尔难题,jquery,if-statement,cookies,boolean,toggleclass,Jquery,If Statement,Cookies,Boolean,Toggleclass,让下面的if语句检查cookie的存在以及它是真是假。由于某种原因,它不起作用 代码如下: // the if statement if($.cookie('style-widget-state') === true){ $('.style-widget').addClass('style-toggle-show'); }; // setting the cookie on a toggleClass method $('.style-tab').on('click',
// the if statement
if($.cookie('style-widget-state') === true){
$('.style-widget').addClass('style-toggle-show');
};
// setting the cookie on a toggleClass method
$('.style-tab').on('click', function() {
$(".style-widget").toggleClass("style-toggle-show");
$.cookie('style-widget-state', $('.style-widget').hasClass('style-toggle-show'), {expires:365, path: '/'});
$(this).toggleClass("ai-cogs ai-arrow-left5");
return false;
});
非常感谢您的帮助。在cookies中,值存储为字符串,因此使用
==
进行比较将失败,请尝试
if($.cookie('style-widget-state') === 'true'){
}
演示:未意识到cookie值存储在字符串中。。。假设布尔语从来不是弦。非常感谢。