使用jQuery保留切换状态 [英] Preserve toggle state using jQuery

查看:97
本文介绍了使用jQuery保留切换状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jQuery与Cookie切换

Possible Duplicate:
jQuery Toggle with Cookie

我有一个简单的切换,但是切换总是在页面刷新时重置,所以我想通过设置cookie来保留其状态,但是没有太多想法该怎么做...

Am having a simple toggle, but the toggle always resets when the page refreshes, so I want to preserve its state by setting a cookie but am not having much Idea how should I do it...

任何想法如何保持切换状态?

Any idea how do I preserve the state of toggle?

http://jsfiddle.net/Qyufx/

<div class="question_trigger">
   <p>Hi</p>
</div>
<div class="toggle_container">
   <div class="block">Hello</div>
</div>


jQuery(document).ready(function(){
        jQuery(".toggle_container").hide();
        jQuery("div.question_trigger").click(function(){
        jQuery(this).toggleClass("active").next().toggle("slow");
    });
});​

推荐答案

使用cookie的解决方案:

Solution using cookie:

jQuery(document).ready(function() {
    jQuery(".toggle_container").toggle($.cookie('showTop') != 'collapsed');
    jQuery("div.question_trigger").click(function() {
        jQuery(this).toggleClass("active").next().toggle();
        var new_value = $(".toggle_container").is(":visible") ? 'expanded' : 'collapsed';
        $.cookie('showTop', new_value);
    });
});​

FIDDLE

这篇关于使用jQuery保留切换状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆