简体   繁体   English

实施cookie以保存JQuery切换状态

[英]Implementing cookie to save state of JQuery toggle

I would really appreciate some help implementing a cookie to save the state of a toggle in JQuery, I have created the toggle using he code in the fiddle below and have tried implementing the answer from BARMAR in the thread below but this changes the state of the tags just below it and closes the tags every time the page is loaded when I try to implement it. 我真的很感谢在cookie中实现cookie来保存切换状态的帮助,我在下面的小提琴中使用他的代码创建了切换,并尝试在下面的线程中实现BARMAR的答案,但这改变了状态标签在它下面,并且在每次尝试实现该页面时都会在每次加载页面时关闭标签。

Thread I have looked at: Preserve toggle state using jQuery 我看过的主题: 使用jQuery保留切换状态

My Fiddle thus far: 到目前为止,我的小提琴:

<script type="text/javascript">
$(function() {
    $('#slide1').click(function() {
        $('.slide1').slideToggle('slow');
        $('.toggle1').toggle();
        return false;
    });
});
</script>

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

http://jsfiddle.net/fE6DJ/1/ http://jsfiddle.net/fE6DJ/1/

Any help would be appreciated, 任何帮助,将不胜感激,

Thanks! 谢谢!

Cozmoz, Cozmoz,

You can use the "cookie" plugin referenced here: http://plugins.jquery.com/cookie/ To implement it in your solution, add the javascript file of the plugin in your sources and don't forget to reference it from your page using it. 您可以使用此处引用的“ cookie”插件: http : //plugins.jquery.com/cookie/要在您的解决方案中实现,请在您的源代码中添加该插件的javascript文件,不要忘记从您的参考文件中引用它。使用它的页面。

Then, a few javascript: 然后,一些JavaScript:

$(document).ready(function() {
    // check the cookie when the page loads, opens it if needed (hidden by default)
    alert($.cookie('currentToggle'));
    if ($.cookie('currentToggle') === 'visible') {
        togglePanel(true);
    }

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

function togglePanel(show) {
    var panel = $('.slide1panel');
    var button = $('#slide1');
    if (show) {
        panel.slideToggle('slow');
        button.text('Slide Toggle -');
        $.cookie('currentToggle', 'visible', { path: '/' });
    } else {
        panel.slideToggle('slow');
        button.text('Slide Toggle +');
        $.cookie('currentToggle', 'hidden', { path: '/' });
    }
}

With your html simplified to: 与您的html简化为:

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

Update of your fiddle: http://jsfiddle.net/fE6DJ/5/ 您的提琴更新: http : //jsfiddle.net/fE6DJ/5/

It's working ;-) 它正在工作;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM