从localstorage javascript中删除值

问题描述 投票:4回答:2

我试图在javascript中从localStorage中删除项目,但它不起作用。

它仅适用于以下事件的序列

Right click --> Inspect element --> Resources --> localStorage --> Right click --> Delete 

我试过了:

localStorage.clear(); 

localStorage.removeItem(key); // the key is the link of the page and 
                              // the value is the selected word in the page 

并且两个都不起作用,我可以使用localStorage.setItem(key, vaule)保存项目并使用localStorage.getItem(key)从localStorage获取项目但我无法删除它们。

javascript local-storage
2个回答
4
投票

你要找的是这个:

deleter void removeItem(DOMString key);

您可以这样实现:

localStorage.removeItem(key)

关于本地存储的一切都可以找到here on the W3 html5 spec for web storage.

你可能会发现这个document more helpful as it goes into more explanation.

*本地存储不是完成的规范,这些可能会改变。


-2
投票

没有()的localStorage.clear目前适用于主流浏览器。

© www.soinside.com 2019 - 2024. All rights reserved.