【问题标题】:Having trouble implementing cookie save state to JQuery toggle无法将 cookie 保存状态实现到 JQuery 切换
【发布时间】:2014-08-16 07:01:12
【问题描述】:

我在 Nicolas R 的帮助下想出了一个 JQuery 切换,它使用 cookie 保存切换的状态,但目前在实现它时遇到了麻烦,因为一旦激活它就会为所有按钮拉入相同的标题.

请在下面找到:

HTML:

<div>
    <a href="#" id="slide1">Slide Toggle +</a>
    <div id="slide1panel" style="display:none; background-color:#4CF;width:100px;height:200px;"></div>
</div>

<div>
    <a href="#" id="slide2">Slide Toggle +</a>
    <div id="slide2panel" style="display:none; background-color:#4CF;width:100px;height:200px;"></div>
</div>

jQuery

$(document).ready(function() {
    // check the cookies when the page loads
    // 1st panel
    if ($.cookie('currentToggleslide1panel') === 'visible') {
        togglePanel($('#slide1panel'), $('#slide1'), true, 0);
    }
    // 2nd panel
     if ($.cookie('currentToggleslide2panel') === 'visible') {
        togglePanel($('#slide2panel'), $('#slide2'), true, 0);
    }    

    //handle the clicking of the show/hide toggle button of the 1st panel
    $('#slide1').click(function() {
        //toggle the panel as required, base on current state
        if ($('#slide1').text() === "Slide Toggle +") {
            togglePanel($('#slide1panel'), $('#slide1'), true, 'slow');
        }
        else {
            togglePanel($('#slide1panel'), $('#slide1'), false, 'slow');
        }
    });

     //handle the clicking of the show/hide toggle button of the 2nd panel
    $('#slide2').click(function() {
        //toggle the panel as required, base on current state
        if ($('#slide2').text() === "Slide Toggle +") {
            togglePanel($('#slide2panel'), $('#slide2'), true, 'slow');
        }
        else {
            togglePanel($('#slide2panel'), $('#slide2'), false, 'slow');
        }
    });
});

function togglePanel(panel, button, show, toggleSpeed) {
    if(toggleSpeed > 0 || toggleSpeed === 'slow' || toggleSpeed === 'fast') {
        panel.slideToggle(toggleSpeed);
    } else {
        panel.toggle();
    }

    if (show) {
        // Set a cookie containing the panel name
        $.cookie('currentToggle' + panel.attr('id'), 'visible', { path: '/' });
        button.text('Slide Toggle -');
    } else {
        // Set a cookie containing the panel name
        $.cookie('currentToggle' + panel.attr('id'), 'hidden', { path: '/' });
        button.text('Slide Toggle +');
    }
}

JS Fiddle

谢谢!

【问题讨论】:

    标签: javascript jquery cookies


    【解决方案1】:

    科兹莫兹,

    我在您的 JS Fiddle 中更新了引用的 cookie 脚本(请参阅外部资源):它现在似乎可以工作了。当我第一次尝试时,cookie 导致了一个错误。 文本是独立变化的,你能检查一下你是否有错误吗?

    编辑:新响应:面板之间的文本不同,在此示例中,我使用当前文本并将最后一个字符替换为加号/减号

    http://jsfiddle.net/xVa7e/6/

    $(document).ready(function() {
        // check the cookies when the page loads
        // 1st panel
        if ($.cookie('currentToggleslide1panel') === 'visible') {
            togglePanel($('#slide1panel'), $('#slide1'), 0);
        }
        // 2nd panel
         if ($.cookie('currentToggleslide2panel') === 'visible') {
            togglePanel($('#slide2panel'), $('#slide2'), 0);
        }    
    
        //handle the clicking of the show/hide toggle button of the 1st panel
        $('#slide1').click(function() {
            //toggle the panel as required
            togglePanel($('#slide1panel'), $('#slide1'), 'slow');
        });
    
         //handle the clicking of the show/hide toggle button of the 2nd panel
        $('#slide2').click(function() {
            //toggle the panel as required
            togglePanel($('#slide2panel'), $('#slide2'), 'slow');
        });
    });
    
    function togglePanel(panel, button, toggleSpeed) {
        var panelPreviousStateVisible = panel.is(':visible');
        // Toggle the div
        if (toggleSpeed > 0 || toggleSpeed === 'slow' || toggleSpeed === 'fast') {
            panel.slideToggle(toggleSpeed);
        } else {
            panel.toggle();
        }
    
        // Once toggled, set the cookie and the text
        if (!panelPreviousStateVisible) { 
            $.cookie('currentToggle' + panel.attr('id'), 'visible', { path: '/' });
            // Set the text by removing the last char and add a minus symbol
            button.text(button.text().slice(0,-1) + "-");
        } else {
             $.cookie('currentToggle' + panel.attr('id'), 'hidden', { path: '/' });
            // Set the text by removing the last char and add a plus symbol
            button.text(button.text().slice(0,-1) + "+");
        }
    }
    

    【讨论】:

    • 你的意思是把fiddle中的外部资源更新为cookie的另一个副本吗?我不确定 Fiddle 中的代码是否存在错误。公平地说,它主要是你的代码。你如何让它独立更新?您有所做更改的副本吗?非常感谢。
    • 我更改了引用的,是的,引用的文件不是我的,它是网络插件。新源来自cdnjs.com/libraries/jquery-cookie 代码没有变化,因为我没遇到问题
    • 原始示例是:raw.githubusercontent.com/carhartl/jquery-cookie/master/… 实际上文件是相同的,但我不明白为什么 jsfiddle 不再适用于第一个
    • 好的,谢谢,您介意上传一个使用不同名称的类别的小提琴吗,我真的很想看看您实现这个的方式! :-)
    • 对不起 Cozmoz 我不明白你的最后一点:我应该实现什么?您指的是哪些类别?
    猜你喜欢
    • 2014-08-13
    • 2014-05-22
    • 1970-01-01
    • 2013-08-16
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    相关资源
    最近更新 更多