使用 Jquery 烘焙 cookie

发布于 2024-11-09 03:34:57 字数 898 浏览 0 评论 0原文

有人可以告诉我为什么这不起作用:

$(document).ready(function() {
    $("#cookie").text("expanded");
    //panel toggle
    $(".btn-slide").click(function() {
        $("#panel").slideToggle("slow");
        //set the name and value of the cookie
        $.cookie('panel', 'collapsed');
        $("#cookie").text("collapsed");
    }, function() {
        $("#panel").slideDown("slow");
        //set the name and value of the cookie
        $.cookie('panel', 'expanded');
        $("#cookie").text("expanded");
    });
    // cookie
    var panel = $.cookie('panel');
    // Set the user's selection for the state
    if (panel == 'collapsed') {
        $("#panel").hide();
    };
});

jsFiddle 示例

我没有看到任何错误,但在刷新时面板不保持其状态(无论是打开还是关闭)。它只是返回到关闭的默认状态。我正在使用 cookie 插件。

Can someone tell me why this doesn't work:

$(document).ready(function() {
    $("#cookie").text("expanded");
    //panel toggle
    $(".btn-slide").click(function() {
        $("#panel").slideToggle("slow");
        //set the name and value of the cookie
        $.cookie('panel', 'collapsed');
        $("#cookie").text("collapsed");
    }, function() {
        $("#panel").slideDown("slow");
        //set the name and value of the cookie
        $.cookie('panel', 'expanded');
        $("#cookie").text("expanded");
    });
    // cookie
    var panel = $.cookie('panel');
    // Set the user's selection for the state
    if (panel == 'collapsed') {
        $("#panel").hide();
    };
});

jsFiddle Example

I don't see anyting wrong, but on refresh the panel doesn't keep it's state (whether open or closed). It just returns to the closed default state. I'm using a plugin for the cookies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2024-11-16 03:34:57

插件行为按预期工作。问题是您误解了 click() 事件的工作原理。

您已经给了它两个函数参数,假设它们将被交替调用来设置 cookie 值。由于只调用了第二个方法,因此您的状态将始终扩展

此外,您还使用 CSS 在面板上设置了 display: none;,这意味着即使设置了正确的状态,它也不会展开。因为在你的JS中,你只检查它是否折叠并隐藏它。

一旦解决了这些问题,它就会按预期工作。这是更新后的代码。

$(document).ready(function() {
    $("#cookie").text("expanded");

    $(".btn-slide").click(function() {
        $("#panel").slideToggle("slow");
        var state = ($.cookie('panel') != 'collapsed') ? 'collapsed' : 'expanded';
        $.cookie('panel', state);
        $("#cookie").text("collapsed");
    });

    var panel = $.cookie('panel');

    if (!panel) {
        $.cookie('panel', 'expanded');
    }

    if (panel == 'collapsed') {
        $("#panel").hide();
    } else {
        $("#panel").slideDown();
    };
});

这是小提琴: http://jsfiddle.net/RJMhT/157/

请记住,我尚未设置默认值,因此对于初始加载,它将被扩展。一旦你真正改变了状态,它就会记住它。

The plugin behavior works as expected. The problem is that you've misunderstood how the click() event works.

You've given it two function arguments assuming that they will be called in alternatively to set the cookie values. Since only the second method is ever called, your state will always be expanded.

Also, you've set display: none; on the panel with your CSS, which means it won't expand even if the proper state was set. Because in your JS, you only check whether it's collapsed and hide it.

It works as expected once you fix these issues. Here's the updated code.

$(document).ready(function() {
    $("#cookie").text("expanded");

    $(".btn-slide").click(function() {
        $("#panel").slideToggle("slow");
        var state = ($.cookie('panel') != 'collapsed') ? 'collapsed' : 'expanded';
        $.cookie('panel', state);
        $("#cookie").text("collapsed");
    });

    var panel = $.cookie('panel');

    if (!panel) {
        $.cookie('panel', 'expanded');
    }

    if (panel == 'collapsed') {
        $("#panel").hide();
    } else {
        $("#panel").slideDown();
    };
});

And here's the fiddle : http://jsfiddle.net/RJMhT/157/

Keep in mind that I haven't set a default, so for the initial loads it will be expanded. And once you've actually changed the state it will remember it.

滥情空心 2024-11-16 03:34:57

你好!

对我来说这个问题很清楚!

工作示例:

$(document).ready(function() {

    // default:
    $("#panel").hide();
    $('#cookie').text('collapsed');

    // if cookie exist:
    if ($.cookie('panel') == 'open') {
        $('#panel').show();
        $('.slide').addClass('expanded');
        $('#cookie').text('expanded');
    }

    $(".slide").click(function() {
        $(this).toggleClass('expanded'); // toggle class
        if ($(this).hasClass('expanded')) { // now we check what happend:
            $.cookie('panel', 'open', {expires: 1} ); //create cookie if .expanded is added to button
            $('#cookie').text('expanded');
        }
        else {
            $.cookie('panel', null, {expires: 1} ); // else: delete cookie
            $('#cookie').text('collapsed');
        };
        $(this).prev('#panel').slideToggle(800);
    });
});

这是一个 JSFiddle 演示

HI!

Well to me the question was pretty clear!

Working Ex:

$(document).ready(function() {

    // default:
    $("#panel").hide();
    $('#cookie').text('collapsed');

    // if cookie exist:
    if ($.cookie('panel') == 'open') {
        $('#panel').show();
        $('.slide').addClass('expanded');
        $('#cookie').text('expanded');
    }

    $(".slide").click(function() {
        $(this).toggleClass('expanded'); // toggle class
        if ($(this).hasClass('expanded')) { // now we check what happend:
            $.cookie('panel', 'open', {expires: 1} ); //create cookie if .expanded is added to button
            $('#cookie').text('expanded');
        }
        else {
            $.cookie('panel', null, {expires: 1} ); // else: delete cookie
            $('#cookie').text('collapsed');
        };
        $(this).prev('#panel').slideToggle(800);
    });
});

And here is a JSFiddle DEMO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文