【问题标题】:jQuery toggle cookie functionjQuery 切换 cookie 功能
【发布时间】:2011-10-27 23:26:27
【问题描述】:

我这里有简单的切换(按钮切换)功能,但我不知道如何添加 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");
    });

}); 
});

【问题讨论】:

  • 请参阅PPK's article on cookies - 希望它提供的信息足以提供帮助。
  • 那里需要 cookie 做什么?
  • OP 希望在用户再次访问该页面时记住“视图”

标签: javascript jquery cookies toggle


【解决方案1】:

看看这个插件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>

【讨论】:

  • 我尝试但无法理解如何制作这个:(
  • 是的,这可以工作,但我添加这个有问题,检查我的脚本..pastebin.com/eNUvWs4R 当我在这里尝试 cookie 时,有时需要点击两次按钮来更改视图
【解决方案2】:

这个呢?这将根据事实将 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;
}

【讨论】:

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