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