我希望能够从本地存储中删除值而不是键,有什么线索吗?
$(".delete").on("click", function() {
localStorage.removeItem('Property');
});
最佳答案
尝试使用setItem()并将值设置为 null
或 blank
,例如,
$(".delete").on("click", function() {
localStorage.setItem('Property',"");
});
更新按照@qutz评论,如果您有多个项目,那么首先您需要以 JSON 格式存储值,然后您将能够解析和拼接以删除单个项目,例如,
$(".delete").on("click", function() {
var ar = JSON.parse(localStorage.getItem("Property")),
item='color';// let we wants to remove color property from json array
var index = ar.indexOf(item);
if (index > -1) { // if color property found
ar.splice(index, 1);// then remove it
}
// again set the property without having color
localStorage.setItem("Property", JSON.stringify(ar));
});
希望这会对您有所帮助,根据您使用的逻辑和方法,可能需要进行一些更改。
关于javascript - storage.removeItem - 我如何使用它从值而不是键中删除多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41541850/