📅  最后修改于: 2023-12-03 14:41:50.449000             🧑  作者: Mango
localStorage 属性允许在浏览器关闭后,依旧将数据存储在本地。
localStorage.setItem(key, value)
localStorage.getItem(key)
localStorage.removeItem(key)
localStorage.clear()
其中,key 和 value 都为字符串类型。
localStorage.setItem('username', 'Tom');
localStorage.setItem('age', '25');
// 获取单个数据
const username = localStorage.getItem('username');
console.log(username);
// 获取所有数据
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
const value = localStorage.getItem(key);
console.log(key, value);
}
localStorage.removeItem('username');
localStorage.clear();