2013-10-31 2 views
0

단추를 클릭하면 그의 ID가 localstorage에 저장되고 스타일이 변경되는 webapp를 만들려고합니다. 그러나 localstorage에 마지막 버튼을 클릭하면 그 버튼의 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'); 
     } 
    }); 
}); 

이 제발 도와주세요, 감사

+1

무언가를 클릭 할 때마다 '클릭'처리기가 실행됩니다. 다른 행'localStorage.getItem'과'$ ("li a"). each'는 한 번만 실행됩니다. –

답변

0

로컬 스토리지는 키 - 값 저장소입니다. 키를 설정하면 해당 키의 값이 이미 설정되어있는 경우이를 바꿉니다. 그러나, 당신은 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)