我正在尝试创建一个webapp,您将在一个按钮中单击该应用程序,并将他的ID保存到本地存储中,并更改样式。但是当我保存在本地存储中时,单击最后一个按钮,替换它前面的按钮的ID。我的代码:
$(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');
}
});
});请帮帮我,谢谢
发布于 2013-10-31 20:35:53
localStorage是一个键值存储.通过设置键,如果已设置该键,则替换该键的值。但是,您可以将一个字符串化的数组存储在localStorage中,然后检索它并在需要时添加到其中。
//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)https://stackoverflow.com/questions/19715948
复制相似问题