实现jQuery Cookie插件 [英] Implementing jQuery Cookie Plugin

查看:153
本文介绍了实现jQuery Cookie插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jQuery的这个片段中实现jQuery Cookie插件,这样在离开页面时就可以保存切换的打开/关闭状态?

  $(document).ready(function(){
$('a.toggle')click(function(){
var id = $(this).attr ('name');
$('#module'+ id).slideToggle('fast');
$('a.toggle [name ='+ id +']')。toggle );
return false;
});
});


解决方案

这应该保存状态, t在动画期间关闭标签/窗口。如果你担心,这不会很难修复。

  $(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',//使用回调所需的速度
function(){set_cookie(this,'link_'+ id}
);
return false;
});
});

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

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).slideToggle('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: '/' });
}

这篇关于实现jQuery Cookie插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆