本地存储问题 [英] Localstorage troubles
本文介绍了本地存储问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个webapp,您将点击一个按钮,并将其ID保存到本地存储中,然后更改样式。
但是,当我保存在本地存储中时,最后一个我点击的按钮,替换之前的按钮ID。
我的代码:
I am trying to create a webapp, which you will click in a button, and his ID is saved to the localstorage, and change the style. But when I save in localstorage, the last button that i have click, replace the button´s ID before it. My code:
$(document).ready(function(){
$("li a").click(function(){
$("li a").removeClass('active');
$(this).addClass('active');
localStorage.setItem('ativo', $(this).html())
});
var ativo = localStorage.getItem('ativo');
$("li a").each(function(index){
if($(this).html() == ativo) {
$(this).addClass('active');
}
});
});
请帮助我,谢谢
Please help me, thanks
推荐答案
localStorage是一个键值存储。通过设置一个键,如果该键已经设置,则替换该键的值。但是,您可以在localStorage中存储字符串化数组,然后在需要时将其检索并添加到它中。
localStorage is a key-value store. By setting a key, you replace the value of that key if it is already set. However, you could store a stringified array in localStorage, then retrieve it and add to it when needed.
//Store an array in local storage
localStorage.setItem('arr',JSON.stringify([6,8,9])
//retrieve the array, add to it, then save it again
var arr = JSON.parse(localStorage.getItem('arr'));
arr.push(23)
localStorage.setItem('arr', arr)
这篇关于本地存储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文