标签: javascript html5 local-storage
我使用类似localStorage[title] = text;的密钥存储了一个值我知道我可以通过执行text来回忆var text = localStorage[title]但是如何获得title的值插入localStorage,以便程序知道要获得什么值。有没有办法循环遍历localStorage?
localStorage[title] = text;
text
var text = localStorage[title]
title
localStorage
答案 0 :(得分:2)
[已移除for-in示例]
for-in
localStorage API允许您迭代密钥,我们有length属性和key函数。
length
key
key函数接受一个索引并返回密钥的名称:
var key, value; for (var i = 0; i < localStorage.length; i++) { key = localStorage.key(i); value = localStorage.getItem(key); // use key or value }
试试这个例子here。