开发者

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

开发者 https://www.devze.com 2023-01-23 14:11 出处:网络
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 ins

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

精彩评论

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