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 按鈕 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP