«

js传值操作sessionStorage

时间:2023-12-11 10:29     作者:六度科技     分类: JS


js操作sessionStorage

1.设置sessionStorage

      Storage.set = function(name, val) {
        sessionStorage.setItem(name, val);
      }

2.获取sessionStorage

      Storage.get = function(name) {
        return sessionStorage.getItem(name);
      }

3.删除sessionStorage

      Storage.del = function(name) {
        sessionStorage.removeItem(name);
      }

4.完整代码

<html>
  <head>
    <title>sessionStorage</title>
  </head>
  <body>
    <script>
      const Storage = {};
      Storage.get = function(name) {
        return sessionStorage.getItem(name);
      }
      Storage.set = function(name, val) {
        sessionStorage.setItem(name, val);
      }
      Storage.del = function(name) {
        sessionStorage.removeItem(name);
      }
      Storage.set("status", "OK");
      Storage.set("quit", "...");
      //Storage.del("status");
    </script>
  </body>
</html>