HTML5 存儲(chǔ)和獲取數(shù)據(jù)
在Web Storage API中,實(shí)現(xiàn)本地?cái)?shù)據(jù)的存儲(chǔ)和讀取是非常簡(jiǎn)單的事情,下面通過(guò)實(shí)例展示W(wǎng)eb Storage API的使用方法。在下面的例子中,先往本地存儲(chǔ)中寫(xiě)入數(shù)據(jù),接著會(huì)把這些數(shù)捱在控制臺(tái)中打印出來(lái)。
【例題】存儲(chǔ)和獲取數(shù)據(jù)
代碼如下:
<!DOCTYPE html>
<htinl>
<head>
<meta charset="UTF-8">
<title>webStorage</title〉
<script>
function writeStorage(){
var username = document.getElementByld("username").value;
var password = document.getElementByld("password").value;
// 存儲(chǔ)到storage中
localStorage.username = username;
localStorage.password = password;
}
function updateStorage(){
localStorage.username = "李四";
}
//輸出所有的Storage
function readStorage(){
console.log(localStorage);
}
</script〉
</head>
<body>
用戶名:〈input type="text" id="username" /><br/>
密碼:〈input type="text" id="password" /><br/>
<input type="button" value="寫(xiě)入 webStorage" onclick="writeStorage()" />
<input type="button" value="輸出所有的Storage”onclick="readStorage()">
</body>
</html>
點(diǎn)擊加載更多評(píng)論>>