目录
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const obj1 = {
uname: '小明',
age: 18,
gender: '男'
}
// 增 把对象转化为字符串然后再添加
localStorage.setItem('obj', JSON.stringify(obj1))
// 查
console.log(localStorage.getItem('obj'))
console.log(typeof localStorage.getItem('obj')) //string
// 把字符串转化为对象
const str = localStorage.getItem('obj')
console.log(JSON.parse(str))
</script>
</body>
</html>