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>localStorage</title>
</head>
<body>
<h2>localStorage</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() {
localStorage.setItem('msg', 'Hello!!')
localStorage.setItem('msg2', 666)
localStorage.setItem('person', JSON.stringify(p))
}
function readData() {
console.log(localStorage.getItem('msg'))
console.log(localStorage.getItem('msg2'))
console.log(JSON.parse(localStorage.getItem('person')))
console.log(localStorage.getItem('msg3'))
}
function deleteData() {
localStorage.removeItem('msg')
}
function deleteAllData() {
localStorage.clear()
}
</script>
</body>
</html>