2011-07-28 96 views
0

私の例のようにクッキーを切り替える方法はありますか?私は2つのボタンを持っていましたが、1つだけを使用してトグルしたいと思います。jQueryクラスの切り替えとクッキーの値の切り替え?

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

答えて

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

ほら。私はこれを試みたと誓っていますが、私は構文が間違っているにちがいないでしょう。あまりにも長く画面を凝視していた。ありがとう! – ejay

0

さらに簡素

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