HTML DOM localStorage 属性
htmlweb developmentfront end technology更新于 2024/5/17 23:40:17
HTML DOM localStorage 属性允许用户将键值对存储在本地浏览器中,以供将来引用。键值对在明确删除/清除之前不会丢失。
语法
以下是语法 −
Window.localStorage
此处,localStorage 可以具有以下属性/方法 −
- setItem(‘key’,’value’)
- getItem(‘key’)
- removeItem(‘key’)
让我们看一个 localStorage 属性 的示例 −
示例
<!DOCTYPE html>
<html>
<head>
<title>LocalStorage clear()</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
table,th,td {
border:1px solid black;
border-collapse: collapse;
margin: 0 auto;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>LocalStorage clear( )</legend>
<table>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<tr>
<td>PassKey</td>
<td>ja12ertplo</td>
</tr>
</table>
<input type="button" value="Store Key" onclick="storeData()">
<input type="button" value="Delete Key" onclick="clearData()">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var extStyle = document.getElementById("extStyle");
function clearData(){
localStorage.clear();
divDisplay.textContent = 'Succesfully Deleted';
}
function storeData(){
localStorage.setItem('PassKey','ja12ertplo');
divDisplay.textContent = 'Succesfully Stored';
}
</script>
</body>
</html>
输出
点击"存储键"按钮后 −

点击"删除键"按钮后 −

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

