localStorage的存儲功能
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
function MyClick1(){
var username=$("#txtUserName").val();
var pwd=$("#txtPwd").val();
//sessionStorage存儲周期:當前頁面
// sessionStorage.setItem("k_username",username);
// sessionStorage.setItem("k_pwd",pwd);
localStorage.setItem("k_username",username);
localStorage.setItem("k_pwd",pwd);
alert("存儲成功");
}
function MyClick2(){
// alert(sessionStorage.getItem("k_username"));
// alert(sessionStorage.getItem("k_pwd"));
//localStorage 將內容存在本地,不會隨著瀏覽器窗口的關閉被清楚,可手動刪除
localStorage.removeItem("k_username");
localStorage.clear();
alert(localStorage.getItem("k_username"));
alert(localStorage.getItem("k_pwd"));
}
</script>
</head>
<body>
<input type="text" id="txtUserName"/><br>
<input type="password" id="txtPwd"/><br>
<input type="button" value="按鈕1" onclick="MyClick1();"/>
<input type="button" value="按鈕2" onclick="MyClick2();"/>
</body>
</html>
localStorage的存儲功能