[英]how can I add values into an array without erasing previous values in array using javascript?
我使用JavaScript
為Android Phone-gap創建了數組,然后單擊動態列表中的特定鏈接,然后將鏈接的索引值推入數組。再次運行該應用程序,然后單擊另一個鏈接,當前值(ie.,clicked link -indexvalue(I think - it each time create a new array))
都存儲在數組中。
每次運行該應用程序時,如何維護數組中的先前值?如何將當前值添加到同一數組中?
我的代碼是:
var arr1=new Array();
$(".sidemenu li ").click(function() {
var index = $('li').index(this);
saveIndex(index);
});
function saveIndex(index) {
alert(index);
arr1.push(index);
window.localStorage.setItem("key",JSON.stringify(arr1));
var arr = JSON.parse(window.localStorage.getItem("key"));
alert(arr);
}
請指導我。 提前致謝。
使用這樣的東西:
var my_array = [];
//your code on click
my_array.push(my_value);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.