如何從javascript中獲取存儲在html 5本地存儲中的項目列表?如何從javascript中獲取存儲在html 5本地存儲中的項目列表?
35
A
回答
46
Like other JavaScript objects, you can treat the localStorage object as an associative array. Instead of using the getItem() and setItem() methods, you can simply use square brackets.
localStorage.setItem('test', 'testing 1');
localStorage.setItem('test2', 'testing 2');
localStorage.setItem('test3', 'testing 3');
for(var i in localStorage)
{
console.log(localStorage[i]);
}
//test for firefox 3.6 see if it works
//with this way of iterating it
for(var i=0, len=localStorage.length; i<len; i++) {
var key = localStorage.key(i);
var value = localStorage[key];
console.log(key + " => " + value);
}
這將輸出:
testing 3
testing 2
testing 1
test3 => testing 3
test2 => testing 2
test => testing 1
2
由於localStorage是字符串的鍵值,只需使用JSON對其進行序列化,並在想要檢索它時將其反序列化。
localStorage.setItem("bar", JSON.stringify([1,2,3]));
var foo = localStorage.getItem("bar");
JSON.parse(foo);
// returns [1,2,3]
21
localStorage的是參照對象window.St ORAGE,所以ü可以用它作爲對方的對象:
得到物品的排列與jQuery
Object.keys(localStorage)
獲取長度
Object.keys(localStorage).length
迭代
$.each(localStorage, function(key, value){
.....
})
0
可以使用Object.assign()
:
var data = Object.assign({}, localStorage)
相關問題
- 1. HTML 5本地存儲
- 2. 如何將標籤值存儲在本地存儲中以及如何從本地存儲中獲取?
- 3. 在startPage方法中從ObjectiveC獲取HTML5本地存儲項目
- 4. 我該如何在HTML 5本地存儲中存儲更多的5MB
- 5. JavaScript - 將表單文本存儲在本地存儲中
- 6. AmplifyJS沒有存儲在本地存儲中的JavaScript陣列
- 7. HTML 5本地存儲在Firefox
- 8. 如何在ASP.NET PageLoad事件中獲取HTML本地存儲值?
- 9. 存儲在存儲桶列表中的項目數量
- 10. 編輯本地存儲中的項目
- 11. 在本地存儲中獲取並存儲64mb mp4文件
- 12. (HTML 5)多少本地存儲太多?
- 13. 如何刪除本地存儲器內存儲的特定項目?裏面存放的本地存儲
- 14. Javascript表和本地存儲
- 15. HTML 5本地存儲不能在ie 11(v 11.0.42)中啓用DOM存儲
- 16. 如何從存儲庫中丟棄本地項目?
- 17. AngularJS存儲陣列在本地存儲
- 18. JavaScript從本地存儲器讀取JSON
- 19. DynamoDB:如何存儲項目列表
- 20. 如何將本地存儲中的值存儲在數組中,JavaScript?
- 21. 本地存儲列表鍵
- 22. 我如何保存和如何可以存儲在鉻本地存儲陣列相關的鉻本地存儲
- 23. ActiveAndroid如何從表中獲取單列項並將它們存儲在arraylist中
- 24. 本地草料和角度 - 從本地存儲獲取項目的問題
- 25. 如何在angularjs中搜索存儲在本地存儲中的表單?
- 26. Angular 2如何從存儲中刪除列表項目
- 27. 如何刪除存儲在本地存儲中的數據?
- 28. HTML 5本地數據庫存儲的選項
- 29. 如何隱藏列表項中的NULL值從sencha觸摸存儲中獲取
- 30. 如何從項目中獲取存儲項目的文件路徑?
說什麼?在cookie中? – Neal 2011-03-23 19:45:36