zoukankan      html  css  js  c++  java
  • HTML5的本地存储

    Web Storage 提供了两种存储类型API接口:localStorage 和sessionStorage

    二者的简单区别

    名称 localStorage sessionStorage
    生命周期 除非用户删除,否则永久保存 网页关闭,生命周期结束
    存储位置 本地 浏览器端
         

    Storage事件监听:

    只需要使用HTML5 Web Storage API内置的事件监听器对事件进行控制

    addEventListener()

    要监听的事件代码:

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>使用WebStorage存储事件监听实例-操作LocalStorage数据</title>
        
        <script type="text/javascript">
          window.onload = function(){                   /*对storage对象进行了三次操作 */
              localStorage.clear();
              localStorage.setItem("userData","storage demo");
              localStorage.setItem("userData","storage event demo");
          }
        </script>
      </head>
      <body></body>
    </html>

    监听器

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>监听localStorage事件 </title>
        <script type="text/javascript">
          window.onload = function(){
                window.addEventListener("storage",function(e){
                    console.log(e);
                    },true);
              }
        </script>
      </head>
      <body></body>
    </html>

    监听结果:数据被更改了3次

  • 相关阅读:
    Codeforces Round #629 (Div. 3) (A ~ F)
    st表
    Educational Codeforces Round 81 (Rated for Div. 2)
    hihocoder#1996 : 01匹配
    P2056 [ZJOI2007]捉迷藏
    P2495 [SDOI2011]消耗战
    GUETOJ1335
    优先队列重载比较运算
    CCF认证201909-4 推荐系统
    P3178 [HAOI2015]树上操作
  • 原文地址:https://www.cnblogs.com/wuyinghong/p/3422170.html
Copyright © 2011-2022 走看看