You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
1.1 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>sessionStorage</title>
</head>
<body>
<h2>sessionStorage</h2>
<button onclick="saveData()">点击保存一个数据</button><br/>
<button onclick="readData()">点击读取一个数据</button><br/>
<button onclick="deleteData()">点击删除一个数据</button><br/>
<button onclick="deleteAllData()">点击清空数据</button><br/>
<script type="text/javascript">
let p = {name:'测试' , age:18}
function saveData() {
sessionStorage.setItem('msg', 'Hello!!')
sessionStorage.setItem('msg2', 666)
sessionStorage.setItem('person', JSON.stringify(p))
}
function readData() {
console.log(sessionStorage.getItem('msg'))
console.log(sessionStorage.getItem('msg2'))
console.log(JSON.parse(sessionStorage.getItem('person')))
console.log(sessionStorage.getItem('msg3'))
}
function deleteData() {
sessionStorage.removeItem('msg')
}
function deleteAllData() {
sessionStorage.clear()
}
</script>
</body>
</html>