gpt4 book ai didi

javascript - 如何在事先不知道 key 的情况下检索所有 localStorage 项目?

转载 作者:行者123 更新时间:2023-12-02 15:53:44 26 4
gpt4 key购买 nike

我想显示之前编写的所有 key 和存储。我的代码如下。我创建了一个函数(allStorage)但它不起作用。我怎样才能做到这一点?

    function storeUserScribble(id) {
var scribble = document.getElementById('scribble').innerHTML;
localStorage.setItem('userScribble',scribble);
}

function getUserScribble() {
if ( localStorage.getItem('userScribble')) {
var scribble = localStorage.getItem('userScribble');
}
else {
var scribble = 'You can scribble directly on this sticky... and I will also remember your message the next time you visit my blog!';
}
document.getElementById('scribble').innerHTML = scribble;
}

function clearLocal() {
localStorage.clear();
return false;
}

function allStorage() {
var archive = [];
for (var i = 0; i<localStorage.length; i++) {
archive[i] = localStorage.getItem(localStorage.key(i));
}
}

最佳答案

ES2015+ 中最简单的方法是:

const items = { ...localStorage };

关于javascript - 如何在事先不知道 key 的情况下检索所有 localStorage 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com