开发者

In HTML5 client-side storage how do you get the value of the key?

I stored a value with a key like this localStorage[title] = text; I know I can recall the text by doing var 开发者_运维知识库text = localStorage[title] but how do you get the value of the title to insert into the localStorage so that the program knows what value to get. Is there any way to loop through the keys in the localStorage?


[removed for-in example]

The localStorage API, allows you to iterate over the keys, we have a length property, and the key function.

The key function takes an index and returns the name of the key:

var key, value;
for (var i = 0; i < localStorage.length; i++) {
  key = localStorage.key(i);
  value = localStorage.getItem(key);
  // use key or value
}

Try this example here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜