繁体   English   中英

jQuery切换cookie功能

[英]jQuery toggle cookie function

我在这里具有简单的切换(按钮切换)功能,但是我不知道如何添加cookie以及如何在用户单击按钮时记住位置。

有人可以帮我在这里添加cookie吗?

$(document).ready(function(){ 
 $("a.switch_thumb").toggle(function(){
  $(this).addClass("swap"); 
  $("ul.display").fadeOut(100, function() {
  $(this).fadeIn(100).addClass("thumb_view"); 
     });
  },

function () {
 $(this).removeClass("swap");
 $("ul.display").fadeOut(100, function() {
 $(this).fadeIn(110).removeClass("thumb_view");
    });

}); 
});

看看这个插件https://github.com/carhartl/jquery-cookie非常简单,在包含它之后,您可以像添加一个标志

$.cookie('cookie_button', 'pressed'); //when pressed or
$.cookie('cookie_button', 'not_pressed'); //if is the case

然后在加载页面时检查cookie值,以便您可以记住用户的最后选择

将此添加到您的头像

<script>
/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
</script>
<script type="text/javascript">
$(document).ready(function(){


    if($.cookie("button")=="thumb_view"){
    $('ul.display').addClass($.cookie("button"));
    $("a.switch_thumb").addClass("swap");

    }else{
    $('ul.display').removeClass($.cookie("button"));
    $("a.switch_thumb").removeClass("swap");

    }



    $("a.switch_thumb").toggle(function(){

      $(this).addClass("swap"); 
      $("ul.display").fadeOut("fast", function() {
        $(this).fadeIn("fast").addClass("thumb_view");          
         });
      $.cookie("button", "thumb_view");  

      }, function () {
      $.cookie("button", "not_thumb_view");
      $(this).removeClass("swap");
      $("ul.display").fadeOut("fast", function() {
        $(this).fadeIn("fast").removeClass("thumb_view");

        }); 
      $.cookie("button", "not_thumb_view");

    }); 




});
</script>

那这个呢? 这将根据事实将button_cookie设置为值1或0,无论用户单击按钮的次数是偶数还是奇数

$(document).ready(function(){ 
    $("a.switch_thumb").toggle(
        function(){
            $(this).addClass("swap"); 
            $("ul.display").fadeOut(100, function() {
                $(this).fadeIn(100).addClass("thumb_view"); 
            });
            setCookie('button_pressed',!$(this).hasClass("swap"));
        },
        function () {
            $(this).removeClass("swap");
            $("ul.display").fadeOut(100, function() {
                $(this).fadeIn(110).removeClass("thumb_view");
            });
            setCookie('button_pressed',$(this).hasClass("swap"));
        }
    ); 
});

/**
 * implementation of setCookie function
 * this function creates only session cookie, can be amended so to use expires param
 */
function setCookie(name, value){
    document.cookie=name+'='+value;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM