我怎样才能得到存储在HTML 5本地存储从JavaScript的项目清单?(How can I get a list of the items stored in html 5 local stora

编程入门 行业动态 更新时间:2024-10-25 00:34:20
我怎样才能得到存储在HTML 5本地存储从JavaScript的项目清单?(How can I get a list of the items stored in html 5 local storage from javascript?)

我怎样才能得到存储在HTML 5本地存储从JavaScript的项目清单?

How can I get a list of the items stored in html 5 local storage from javascript?

最满意答案

来自HTML5参考 :

与其他JavaScript对象一样,您可以将localStorage对象视为关联数组。 除了使用getItem()和setItem()方法,您可以简单地使用方括号。

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

这里是JSFiddle演示

From HTML5 reference:

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); }

This will output:

testing 3 testing 2 testing 1 test3 => testing 3 test2 => testing 2 test => testing 1

Here is the JSFiddle Demo

更多推荐

本文发布于:2023-08-05 21:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:清单   项目   list   JavaScript   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!