我怎样才能得到存储在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参考 :
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); }与其他JavaScript对象一样,您可以将localStorage对象视为关联数组。 除了使用getItem()和setItem()方法,您可以简单地使用方括号。
这将输出:
testing 3 testing 2 testing 1 test3 => testing 3 test2 => testing 2 test => testing 1这里是JSFiddle演示
From HTML5 reference:
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); }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.
This will output:
testing 3 testing 2 testing 1 test3 => testing 3 test2 => testing 2 test => testing 1Here is the JSFiddle Demo
更多推荐
发布评论