jQu*_*ast 21 javascript jquery html5 local-storage
我有2个应用程序与localstorage一起工作,我想知道如何删除所有以note-和todo-开头的键.我知道localstorage.clear()会清除所有内容,但这不是我的目标.
以下是我在本地存储中的示例: 
我想删除所有todo-*点击按钮和所有note-*与其他按钮点击使用jquery.
非常感谢
Gho*_*toy 42
Object.keys(localStorage)
.forEach(function(key){
if (/^todo-|^note-/.test(key)) {
localStorage.removeItem(key);
}
});
Run Code Online (Sandbox Code Playgroud)
WEF*_*EFX 10
我使用了与@Ghostoy类似的方法,但我想输入一个参数,因为我在代码中的几个地方调用了它.我无法在正则表达式中使用我的参数名称,所以我只使用了substring.
function ClearSomeLocalStorage(startsWith) {
var myLength = startsWith.length;
Object.keys(localStorage)
.forEach(function(key){
if (key.substring(0,myLength) == startsWith) {
localStorage.removeItem(key);
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24141 次 |
| 最近记录: |