【问题标题】:Implementing jQuery Cookie Plugin实现 jQuery Cookie 插件
【发布时间】:2023-03-15 00:35:01
【问题描述】:
如何将 jQuery Cookie 插件实现到这个 jQuery 的 sn-p 中,以便在离开页面时保存切换的打开/关闭状态?
$(document).ready(function() {
$('a.toggle').click(function() {
var id = $(this).attr('name');
$('#module' + id).slideToggle('fast');
$('a.toggle[name='+id+']').toggle();
return false;
});
});
【问题讨论】:
标签:
jquery
plugins
cookies
toggle
【解决方案1】:
只要他们在动画期间不关闭选项卡/窗口,这应该会保存状态。如果您对此感到担心,那就不难解决了。
$(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: '/' });
}