从本地存储 javascript 中删除值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14864256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove value from localstorage javascript
提问by elianore
I'm trying to remove item from localStoragein javasscript but it doesn't work.
我正在尝试从javascript 中的localStorage中删除项目,但它不起作用。
It only works manually for the sequence of the following events
它仅适用于以下事件的序列
Right click --> Inspect element --> Resources --> localStorage --> Right click --> Delete
I tried:
我试过:
localStorage.clear();
and
和
localStorage.removeItem(key); // the key is the link of the page and
// the value is the selected word in the page
and both didn't work, I can save item using localStorage.setItem(key, vaule)
and get items from localStorage using localStorage.getItem(key)
but I can't remove them.
并且两者都不起作用,我可以使用保存项目localStorage.setItem(key, vaule)
并使用 localStorage 获取项目,localStorage.getItem(key)
但我无法删除它们。
回答by Ryan
what your looking for is this:
你要找的是这个:
deleter void removeItem(DOMString key);
You could implement this like so:
你可以这样实现:
localStorage.removeItem(key)
Everything about local storage can be found here on the W3 html5 spec for web storage.
关于本地存储的所有信息都可以在 W3 html5 网络存储规范中找到。
You may find this document more helpful as it goes into more explanation.
您可能会发现此文档更有用,因为它有更多解释。
*Local storage is not a finished spec and these could change.
*本地存储不是一个完成的规范,这些可能会改变。
回答by mdub
localStorage.clear without the () currently works in major browsers.
没有 () 的 localStorage.clear 目前适用于主要浏览器。