开发者

jQuery toggle of classes and toggle the cookie value?

开发者 https://www.devze.com 2023-03-24 03:07 出处:网络
Is there a possible way to toggle the cookie like my example below?I used to have two buttons but would like to just use one and toggles.

Is there a possible way to toggle the cookie like my example below? I used to have two buttons but would like to just use one and toggles.

$("#text-change").click(function() {
    $("body").toggleClass("large");
    $(this).toggleClass("large");

    // Here I want to toggle the cookie value
    $.cookie("textSize", "large", {expires: 365});
    $.cookie("textSize", "small", {expires: 365});

    return false;
});

//then I can the check cookie throughout the site
if($.cookie("textSize") != "large") {
    $("#text-smaller").addClass("disabled");
    $("body").removeClass("large");
}
else {
    $("#text-larger").addClass("disable开发者_如何学编程d");
    $("body").addClass("large");
}


$("#text-change").click(function() {
    $("body").toggleClass("large");
    $(this).toggleClass("large");

    // Here I want to toggle the cookie value
    if ($(this).hasClass("large")){
        $.cookie("textSize", "large", {expires: 365});
    }else{
        $.cookie("textSize", "small", {expires: 365});
    }

    return false;
});


Even more simpler

$("#text-change").click(function() {
    $("body").add(this).toggleClass("large");

    // Here I want to toggle the cookie value
    $.cookie("textSize", $(this).hasClass("large")?"large":"small", {expires: 365});

    return false;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号