localStorage是永久存储
(关闭页面值不会丢失)
登录的时候可以记住用户名密码。方便下次登录
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btn">存</button>
<button id="btn2">查</button>
<button id="btn3">删除</button>
<button id="btn4">清空</button>
<script>
btn.onclick = function(){
// console.log()
// 只能存字符串
localStorage.setItem("name","xiaoming")
}
btn2.onclick = function(){
console.log(localStorage.getItem("name"))
}
btn3.onclick = function(){
localStorage.removeItem("name")
}
btn4.onclick = function(){
localStorage.clear()
}
</script>
</body>
</html>
sessionStorage是临时存储
(关闭页面值就丢失)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btn">存</button>
<button id="btn2">查</button>
<button id="btn3">删除</button>
<button id="btn4">清空</button>
<script>
btn.onclick = function(){
// console.log()
// 只能存字符串
sessionStorage.setItem("name","xiaoming")
}
btn2.onclick = function(){
console.log(sessionStorage.getItem("name"))
}
btn3.onclick = function(){
sessionStorage.removeItem("name")
}
btn4.onclick = function(){
sessionStorage.clear()
}
</script>
</body>
</html>