使用jQuery和cookies在两个div之间切换

4
我有一个函数,可以使用按钮(#button)在两个 div (.grid.list)之间进行切换。
HTML:
<a href="#" id="button">Change view</a>
<div class="grid">GRID</div>
<div class="list">LIST</div>

jQuery:
$('.list').hide();
$('.grid').show();
$('#button').toggle(function() {
    $('.grid').hide();
    $('.list').show();
    return false;
}, function() {
    $('.list').hide();
    $('.grid').show();
    return false;
});

如何添加 cookie 支持,以便在页面加载后保存和显示切换状态?当用户第一次加载页面时,应显示网格视图。
我尝试了之前的许多选项,但它们都失败了。

有很多变量,但把它们全部删除了。发表不工作的代码片段没有用。 - Sergey
你的 toggle(); 使用很棒,给你点个赞。 - thwd
我的 toggle(); 有问题吗? - Sergey
我认为你可以用切换按钮做一些更简单的事情! :) - Nicola Peluchetti
3个回答

1

只需设置和获取 cookie 的值,然后相应地切换元素(示例:http://jsfiddle.net/bpcJd/1/):

function setCookie(name, value, lifetime_days) {
    var exp = new Date();
    exp.setDate(new Date().getDate() + lifetime_days);
    document.cookie = name + '=' + value + ';expires=' + exp.toUTCString() + ';path=/';
}

function getCookie(name) {
    if(document.cookie) {
        var regex = new RegExp(escape(name) + '=([^;]*)', 'gm'),
        matches = regex.exec(document.cookie);
        if(matches) {
            return matches[1];
        }
    }
}

// show list if cookie exists
if(getCookie('showlist')) {
    $('.list').show();
    $('.grid').hide();
} else {
    $('.list').hide();
    $('.grid').show();
}   

// click handler to toggle elements and handle cookie
$('#button').click(function() {
    // check the current state
    if($('.list').is(':hidden')) {
        // set cookie
        setCookie('showlist', '1', 365);
    } else {
        // delete cookie
        setCookie('showlist', '', -1);
    }
    // toggle
    $('.list').toggle();
    $('.grid').toggle();
    return false;
});

我正在尝试您的解决方案,但它似乎只在同一页上起作用。我想将cookie应用于整个站点。我该怎么做?还是我做错了什么? - Sergey
它应该在整个域中工作,因为 path 设置为 / - pstadler
你是不是忘记在 path= 前面加上 ; 了? - Sergey

1
如果您使用jQuery $.cookie,那么可以这样做:
var $layouts = $('.list, .grid'), // cache objects
    $button = $('#button');       // to avoid overhead

$button.click(function(e, className){
    e.preventDefault();
    if(typeof className != 'undefined')
        $('.'+className).hide();
    $layouts.toggle();

    // set cookie to hold the state
    $.cookie('shown_type', ($layouts.eq(0).is(':visible') ? 'list' : 'grid'));
});

// check to see if a cookie exists for the app state
var shown_type = $.cookie('shown_type');
if(shown_type == 'list' || shown_type == 'grid'){
    $button.trigger('click', [shown_type]); // yes, a cookie exist, show this layout
} else{
    $button.trigger('click', ['list']); // no, a cookie does not exist, show list by default
}

演示。为了测试它是否有效,请单击一次开关将其设置为网格,然后复制URL并打开一个新标签页,应该显示网格布局。


0

如果你使用jQuery cookie插件,你可以这样做:

var visible = false;
if ($.cookie('grid') === 'true'){
    visible = true;
}
$('.list').toggle(!visible);
$('.grid').toggle(visible);


$('#button').click(function(){

    $.cookie('grid', $('.grid').is(':visible') , {expires: 365});
    $('.grid').toggle();
    $('.list').toggle();
});

在这里测试: http://jsfiddle.net/aXCSq/

此代码会显示列表并将".grid"的可见性保存在cookie中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接