in cookies the value is stored as a string, so your comparison using ===
will fail, try
if($.cookie('style-widget-state') === 'true'){
}
Demo: Fiddle
質問
Have the following if statement checking for the presence of a cookie and whether it's true or false. For some reason it's not working.
Here's the code:
// 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;
});
Much obliged for any insights.
解決
in cookies the value is stored as a string, so your comparison using ===
will fail, try
if($.cookie('style-widget-state') === 'true'){
}
Demo: Fiddle