HTML DOM Storage removeItem() 方法
htmlweb developmentfront end technology更新于 2025/9/27 19:24:17
HTML DOM Storage removeItem() 方法用于通过传递给定的键名来删除存储对象项。
语法
以下是 Storage removeItem() 方法的语法 −
localStorage.removeItem(keyname);
或
sessionStorage.removeItem(keyname);
其中,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>
输出
这将产生以下输出 −

点击"显示"按钮 −

点击"删除"按钮 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

