如何使用localstorage刷新页面后保存选定的div显示?

时间:2018-08-30 07:31:31

标签: javascript jquery

我已经发布了如何使用本地存储在刷新页面上显示和隐藏div记录?

点击右上角的RUB或UAH后,它会执行脚本以显示和隐藏货币明细,但是重新加载页面后,它又消失了。

这是此脚本的代码:

         function loadAfterTime() {

          $(document).ready(function(){
            $("#uahid").click(function(){
                $("#total > div:nth-child(1), #c_cost > b:nth-child(2), #c_cost > b:nth-child(1), #local_delivery_prices > div:nth-child(1)").show();
                $("#total > div:nth-child(3), #c_cost > b:nth-child(8), #c_cost > b:nth-child(7), #local_delivery_prices > div:nth-child(3), #c_cost > b:nth-child(5)").hide();
            });
         });


         $(document).ready(function(){
            $("#rubid").click(function(){
                $("#total > div:nth-child(3), #c_cost > b:nth-child(8), #c_cost > b:nth-child(7), #local_delivery_prices > div:nth-child(3), #c_cost > b:nth-child(5)").show();
                $("#total > div:nth-child(1), #c_cost > b:nth-child(2), #c_cost > b:nth-child(1), #local_delivery_prices > div:nth-child(1)").hide();
            });
         });

         }

1 个答案:

答案 0 :(得分:1)

您可以为这些div分配一个唯一的ID,并在更改它们时将这些ID的每个状态隐藏或显示在localStorage中。也许在每次隐藏/显示呼叫之后,您都可以执行localStorage.setItem('item' + ID, state)。将state等于隐藏或显示,然后刷新后阅读,以了解其之前的状态并应用它。