【问题标题】:jQuery toggle of classes and toggle the cookie value?jQuery切换类并切换cookie值?
【发布时间】:2011-07-29 02:21:50
【问题描述】:

有没有办法像我下面的例子那样切换 cookie?我曾经有两个按钮,但我只想使用一个来切换。

$("#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("disabled");
    $("body").addClass("large");
}

【问题讨论】:

    标签: jquery cookies toggle


    【解决方案1】:
    $("#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;
    });
    

    【讨论】:

    • 哈。我发誓我尝试过这个,但我一定是语法错误。盯着屏幕太久了。谢谢!
    【解决方案2】:

    更简单

    $("#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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多