有没有办法用javascript重置/清除浏览器的localStorage?
当前回答
手动按钮:
<script>
function ask() {
if (confirm('Clear localStorage?') == true) {
localStorage.clear()
location.reload()
}
else {
alert('Nothing happend')
}
}
}
</script>
<style>
button {border-width:0px;background-color:#efefef;padding:5px;width:5cm;margin:5px;}
</style>
<button onclick=ask()>Clear localStorage</button>
其他回答
清除sessionStorage
sessionStorage.clear();
下面是一个简单的代码,它将使用javascript清除存储在浏览器中的本地存储
<script type="text/javascript">
if(localStorage) { // Check if the localStorage object exists
localStorage.clear() //clears the localstorage
} else {
alert("Sorry, no local storage."); //an alert if localstorage is non-existing
}
</script>
要确认本地存储是否为空,请使用以下代码:
<script type="text/javascript">
// Check if the localStorage object exists
if(localStorage) {
alert("Am still here, " + localStorage.getItem("your object name")); //put the object name
} else {
alert("Sorry, i've been deleted ."); //an alert
}
</script>
如果返回null,则清除本地存储。
如果需要清除本地存储数据,请使用:
localStorage.clear()
要从本地存储中删除特定项目,请执行以下操作:
localStorage.removeItem('Item')
如果要清除存储在localStorage中的所有项目,则
localStorage.clear();
使用此选项清除所有存储的密钥。
如果您只想清除/删除特定的键/值,则可以使用removeItem(键)。
localStorage.removeItem('yourKey');
localStorage.clear();
or
window.localStorage.clear();
清除特定项目
window.localStorage.removeItem("item_name");
要按id删除特定值,请执行以下操作:
var item_detail = JSON.parse(localStorage.getItem("key_name")) || [];
$.each(item_detail, function(index, obj){
if (key_id == data('key')) {
item_detail.splice(index,1);
localStorage["key_name"] = JSON.stringify(item_detail);
return false;
}
});
推荐文章
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?