可能的重复:
带 Cookie 的 jQuery 切换 https://stackoverflow.com/questions/2523189/jquery-toggle-with-cookie
我有一个简单的切换,但切换总是在页面刷新时重置,所以我想通过设置 cookie 来保留其状态,但我不知道该怎么做...
知道如何保留切换状态吗?
http://jsfiddle.net/Qyufx/ 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的解决方案:
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 http://jsfiddle.net/barmar/Qyufx/14/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)