开发者

Implementing jQuery Cookie Plugin

开发者 https://www.devze.com 2022-12-15 10:40 出处:网络
How would I implement the jQuery Cookie Plugin into this snippet of jQuery, so it saves the open/closed state of the toggles upon leaving the page?

How would I implement the jQuery Cookie Plugin into this snippet of jQuery, so it saves the open/closed state of the toggles upon leaving the page?

$(document).ready(function() {
  $('a.toggle').click(function() { 
   var id = $(this).attr('name');
   $('#module' + id).sl开发者_高级运维ideToggle('fast');
   $('a.toggle[name='+id+']').toggle();
   return false; 
  });
});


This should save the state as long as they don't close the tab/window during the animation. If you're worried about that, it wouldn't be hard to fix.

$(function() {
    $('a.toggle').click(function() { 
        var id = $(this).attr('name');
        $('#module' + id).slideToggle(
            'fast',
            function() { set_cookie(this, 'module_' + id); }
        );
        $('a.toggle[name='+id+']').toggle(
            'normal',  // speed required to use callback
            function() { set_cookie(this, 'link_' + id}
        );
        return false; 
  });
});

function set_cookie(target, name) {
    var is_displayed = $(target).css('display') != 'none';
    $.cookie(name, is_displayed, { expires: 30, path: '/' });
}
0

精彩评论

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