HTML DOM 存储 removeItem() 方法


HTML DOM 存储 removeItem() 方法用于通过传递给定键名,删除存储对象项目。

语法

以下是 Storage removeItem() 方法的语法 -

localStorage.removeItem(keyname);

OR

sessionStorage.removeItem(keyname);

在此处,键名为字符串类型,表示要删除的项目的名称。

示例

让我们看一看 Storage removeItem() 方法的示例 -

<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Storage removeItem() method example</h1>
<p>Delete the localstorage item by clicking the below button</p>
<button onclick="itemDelete>REMOVE</button>
<p>Display the localstorage item by clicking the below button</p>
<button onclick="itemShow>DISPLAY</button>
<p id="Sample"></p>
<script>
   localStorage.setItem("TEXT1","HELLO WORLD");
   function itemDelete() {
      localStorage.removeItem("TEXT1");
      itemShow();
   }
   function itemShow() {
      var x = localStorage.getItem("TEXT1");
      document.getElementById("Sample").innerHTML ="The 'TEXT1' key value is "+x;
   }
</script>
</body>
</html>

输出

这将产生以下输出 -

点击 DISPLAY 按钮 -

点击 REMOVE 按钮 -

更新于: 20-Aug-2019

113 已查看

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.