jQuery切换不会在页面刷新时保持打开 [英] jQuery toggle not staying open when page refreshes

查看:155
本文介绍了jQuery切换不会在页面刷新时保持打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题,当固定边栏中的可折叠菜单在链接被点击时(或当网页简单刷新时)不会保持打开状态。

Having a problem that a collapsible menu in a fixed sidebar is not staying open when the links are clicked (or when the page simply refreshes).

根据此问题提出的关于使用Cookie存储div的状态的建议,但它不工作(切换状态仍然不持续)。

I used the suggestions based on this question about using a cookie to store the div's state but it's not working (toggle state is still not persisting).

我添加了一个指向插件的链接(在我的jQuery src链接之后):

I added a link to the plugin (AFTER my jQuery src link):

<script src="js/jquery.cookie.js" type="text/javascript"></script>

并且切换状态仍然不会保留,即使住在.com网站上。

And the toggle state still doesn't stay, even live on a .com website.

if ($.cookie('div') == 'open'){
    $('#the_more_div').slideDown('slow');
} else {
    $('#the_more_div').slideUp('slow');
}

$('#hamburger').click(function(){
    $('#the_more_div').slideToggle('slow', function(){
        if ($(this).is(':hidden')) {
            $.cookie('div', 'closed');
        } else {
            $.cookie('div', 'open');
        }
    }); 
});

切换效果很好,但为什么不打开当不同的链接点击里面或页刷新)。

The toggle works just fine but why wouldn't the_more_div stay open when different links are clicked inside it (or on page refresh).

提前感谢!

推荐答案

一个无关的冲突。上述代码确实实际上是安装一个cookie来使div状态持续。

Turns out the issue was an unrelated conflict. The above code does in fact work to install a cookie to make the div status persist.

这篇关于jQuery切换不会在页面刷新时保持打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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