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.