简体   繁体   中英

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM