Vue-41、浏览器本地存储

1、local storage

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>localStorage</title>
</head>
<body>
    <h1>localStorage</h1>
    <button onclick="saveData()">点我保存一个数据</button>
    <button onclick="readData()">点我读取一个数据</button>
    <button onclick="deleteData()">点我删除一个数据</button>
    <button onclick="deleteAllData()">点我清空所有数据</button>
<script type="text/javascript">
    let person = {name:'张仨',age:18};
    function saveData() {
        localStorage.setItem('msg','hello');
        localStorage.setItem('msg2',666);
        localStorage.setItem('person',JSON.stringify(person));
    }
    function readData() {
        console.log(localStorage.getItem('msg'));
        console.log(localStorage.getItem('msg2'));
        const result = localStorage.getItem('person');
        console.log(JSON.parse(result));
    }
    function deleteData() {
        localStorage.removeItem('msg');
        localStorage.removeItem('msg2');
        localStorage.removeItem('person');
    }
    function deleteAllData() {
        localStorage.clear();
    }
</script>
</body>
</html>

2、session storage

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>sessionStorage</title>
</head>
<body>
<h1>localStorage</h1>
<button onclick="saveData()">点我保存一个数据</button>
<button onclick="readData()">点我读取一个数据</button>
<button onclick="deleteData()">点我删除一个数据</button>
<button onclick="deleteAllData()">点我清空所有数据</button>
<script type="text/javascript">
    let person = {name:'张仨',age:18};
    function saveData() {
        sessionStorage.setItem('msg','hello');
        sessionStorage.setItem('msg2',666);
        sessionStorage.setItem('person',JSON.stringify(person));
    }
    function readData() {
        console.log(sessionStorage.getItem('msg'));
        console.log(sessionStorage.getItem('msg2'));
        const result = sessionStorage.getItem('person');
        console.log(JSON.parse(result));
    }
    function deleteData() {
        sessionStorage.removeItem('msg');
        sessionStorage.removeItem('msg2');
        sessionStorage.removeItem('person');
    }
    function deleteAllData() {
        localStorage.clear();
    }
</script>
</body>
</html>

3、总结

相关推荐
new出一个对象1 小时前
uniapp接入BMapGL百度地图
javascript·百度·uni-app
你挚爱的强哥2 小时前
✅✅✅【Vue.js】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本
javascript·vue.js·jquery
y先森3 小时前
CSS3中的伸缩盒模型(弹性盒子、弹性布局)之伸缩容器、伸缩项目、主轴方向、主轴换行方式、复合属性flex-flow
前端·css·css3
前端Hardy3 小时前
纯HTML&CSS实现3D旋转地球
前端·javascript·css·3d·html
susu10830189113 小时前
vue3中父div设置display flex,2个子div重叠
前端·javascript·vue.js
IT女孩儿4 小时前
CSS查缺补漏(补充上一条)
前端·css
吃杠碰小鸡5 小时前
commitlint校验git提交信息
前端
天天进步20155 小时前
Vue+Springboot用Websocket实现协同编辑
vue.js·spring boot·websocket
虾球xz5 小时前
游戏引擎学习第20天
前端·学习·游戏引擎
我爱李星璇6 小时前
HTML常用表格与标签
前端·html