我如何显示所有localStorage保存的variables?

我想要访问保存在特定页面上的所有localStoragevariables。 我怎么做? 我想显示它,就像我会显示一个数组与join()函数

您可以尝试迭代localStorage对象中的所有项目:

for (var i = 0; i < localStorage.length; i++){ // do something with localStorage.getItem(localStorage.key(i)); } 

我经常使用这个代码块:

 var i; console.log("local storage"); for (i = 0; i < localStorage.length; i++) { console.log(localStorage.key(i) + "=[" + localStorage.getItem(localStorage.key(i)) + "]"); } console.log("session storage"); for (i = 0; i < sessionStorage.length; i++) { console.log(sessionStorage.key(i) + "=[" + sessionStorage.getItem(sessionStorage.key(i)) + "]"); } 

您也可以在Google chrome开发人员工具中检查localStorage状态和数据

 for(var i in localStorage) { console.log(i + ' = ' + localStorage[i]); }