分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

web-storage

发布时间:2023-09-06 02:08责任编辑:沈小雨关键词:暂无标签

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>离线存储</title>
<link rel="stylesheet" href="">
</head>
<body>
<textarea id="value_text" cols="80" rows="10"></textarea>
<input id="btn_set" value="SET" type="button"></input>
<input id="btn_get" value="GET" type="button"></input>
<script type="text/javascript">
var btnSet = document.querySelector("#btn_set");
var btnGet = document.querySelector("#btn_get");
var txtValue = document.querySelector("#value_text");
btnGet.addEventListener("click",function(){
if (window.localStorage) {//兼容性判断
// txtValue.value = localStorage.getItem("key1");//两种方法,推荐使用
txtValue.value=localStorage[‘key1‘];
}

});
btnSet.addEventListener("click",function(){
if (window.localStorage) {
// localStorage.setItem("key1",txtValue.value);
localStorage[‘key1‘] = txtValue.value;
}

})
</script>
</body>
</html>

localStorage:离线存储,是永久的,如果不删除永远存在,是存储在local storage里面

sessionStorage:暂时的,浏览器关闭后消失,是存储在session storage里面

txtValue.value = localStorage.getItem("key1");
txtValue.value=localStorage[‘key1‘];

两种方法是一样的,但是如果键不存的话,localStorage.getItem返回的是"",localStorage[‘key1‘]返回的是undefined

web-storage

原文地址:https://www.cnblogs.com/wyq-web/p/9420557.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved