切换DIV状态本地存储 [英] Toggle DIV state localstorage

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

问题描述

今天早些时候,我在这里问了一个问题,但是没有正确解释,也没有提供正确的代码.

Earlier today i asked a question on here, but didnt explained it properly and havent provided proper code.

我有2个带链接的切换式DIV,一旦用户打开一个并单击链接,页面就会刷新并显示结果,但是DIV会自行关闭. 我希望它保持打开状态.

I have 2 toggle DIVs with links in them, and once user opens one and clicks on the link, page refresh with results, but that DIV closes itself. I would like it to remain open.

$(document).ready(function() {
            $(".content").hide();
            $(".fa-angle-double-up").hide();
            $(".heading").click(function(){
                    $(this).next(".content").slideToggle(500);
                    $(this).find(".fa-angle-double-up, .fa-angle-double-down").toggle();
            });
        });

有人告诉我可以通过设置cookie或将其存储到本地存储中来完成,但是我不知道(JS新手).如果有人可以帮忙,您会节省我的时间:)

Somebody told me that it can be done either by setting cookies or by storing it into localstorage, but i have no idea (JS newbie).If anybody can help, you would save my day :)

提琴:http://jsfiddle.net/eg7ju4rm/

版主,如果违反规则发表2个具有相同主题的问题,我表示歉意

Moderators, if it is against the rules to post 2 questions with the same topic, i apologize

推荐答案

在点击事件中,您可以添加以下内容:

In your click-event you can add this:

var id = $(this).parent().attr('id');
window.localStorage.setItem('opened', id);

保存打开部分的ID. 并将其添加到您的就绪处理程序中:

to save the ID of the opened section. And add this to your ready-handler:

var opened =  window.localStorage.getItem('opened');
if(opened !== ''){
    $('#' + opened).find('.content').show();
}

演示

Demo

我不知道应该只打开一个div还是打开每个div,我的解决方案只能处理一个打开的div.

I don't know if only one div should be stay open or every opend div, my solutions just handle one open div.

这篇关于切换DIV状态本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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