• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
前端小童鞋
博客园    首页    新随笔    联系   管理    订阅  订阅

web-storage

<!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

 

posted @ 2018-08-04 23:10  前端小童鞋  阅读(129)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3