我有一个表格。我已经使用localStorage.setItem('x', x)
将数组数据存储在localStorage中。
但是如何在localStorage
中编辑或删除这些值?
答案 0 :(得分:0)
设置项目/更新项目
localStorage.setItem('key',value);
阅读项目
var data = localStorage.getItem('key');
删除项目
localStorage.removeItem('key');
删除所有项目
localStorage.clear();
要存储JSON对象
var data = {
name:'john',
age:20
}
localStorage.setItem('data',JSON.stringify(data));
// To get data back
var data = JSON.parse(localStorage.getItem('data'));
// Editing data
data.name = 'mark';
// Saving Back
localStorage.setItem('data',JSON.stringify(data));
答案 1 :(得分:0)
仅需重写原始属性即可进行编辑
localStorage.hello = "Hello World";
localStorage.hello = "Hello World. Part 2.";
localStorage.getItem("hello") // "Hello World. Part 2.";
删除
localStorage.removeItem("hello");