HTML DOM 儲存 setItem() 方法


HTML DOM Storage setItem() 方法用於透過給定的鍵名稱刪除儲存物件項。

語法

以下是 Storage removeItem() 方法的語法 −

localStorage.removeItem(keyname,value);

sessionStorage.removeItem(keyname,value );

此處,keyname 為字串型別,表示用於獲取值的鍵名稱。第二個引數 value 表示將替換舊值的新值。

範例

讓我們看一個 Storage setItem() 方法的範例 −

即時演示

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

輸出

這將產生以下輸出 −

點選 CREATE 按鈕 −

點選 DISPLAY 按鈕 −

更新於: 19-Feb-2021

83 次瀏覽

啟動你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.