本地存储之localStorage
作者:互联网
1.生命周期永久生效,除非手动删除 否则关闭页面也会存在
2.可以多个窗口(页面)共享(同一浏览器可以共享)
3.以键位对的形式存储使用
存储数据:
localStorage.setItem()
获取数据:
localStorage.getItem()
删除数据:
localStorage.removeItem
删除所有数据:
localStorage.clear()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <input type="text"> <button class="set">存储数据</button> <button class="get">获取数据</button> <button class="remove">删除数据</button> <button class="del">清空所有数据</button> <script> var ipt = document.querySelector('input'); var set = document.querySelector('.set'); var get = document.querySelector('.get'); var remove = document.querySelector('.remove'); var del = document.querySelector('.del'); set.addEventListener('click', function() { var val = ipt.value; localStorage.setItem('username', val); }) get.addEventListener('click', function() { console.log(localStorage.getItem('username')); }) remove.addEventListener('click', function() { localStorage.removeItem('username'); }) del.addEventListener('click', function() { localStorage.clear(); }) </script> </body> </html>
标签:function,存储,本地,localStorage,querySelector,var,document,click 来源: https://www.cnblogs.com/0722tian/p/16185143.html