当前位置:网站首页>JS本地存储 sessionStorage和localStorage
JS本地存储 sessionStorage和localStorage
2022-08-09 12:59:00 【_七七】
sessionStorage
<!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> // 关闭浏览器失效 console.log(localStorage.getItem('username')); 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; sessionStorage.setItem('uname', val); sessionStorage.setItem('pwd', val); }); get.addEventListener('click', function () {
// 当我们点击了之后,就可以把表单里面的值获取过来 console.log(sessionStorage.getItem('uname')); }); remove.addEventListener('click', function () {
// sessionStorage.removeItem('uname'); }); del.addEventListener('click', function () {
// 当我们点击了之后,清除所有的 sessionStorage.clear(); }); </script>
</body>
</html>
localStorage
<!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>
边栏推荐
- Record the system calls and C library functions used in this project-2
- 5G China unicom 一般性异常处理
- #WeArePlay | 与更多开发者一起,探索新世界
- Yocto 可以下载的第三方库
- 43. The sword refers to Offer 1 ~ 1 the number of occurrences of n integers (recursive, mathematics)
- 陈强教授《机器学习及R应用》课程 第十七章作业
- Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
- gin's middleware and routing grouping
- Redis源码剖析之字典(dict)
- npm install失败
猜你喜欢
快来扔鸡蛋。
搭建大型分布式服务(二)搭建会员服务
Clock frequency and baud rate count for serial communication in FPGA
Flutter Getting Started and Advanced Tour (7) GestureDetector
Final assignment of R language data analysis in a university
Come and throw eggs.
问题系列-如何修改或更新localhost里的值
[HCIP Continuous Update] Principle and Configuration of IS-IS Protocol
5G China unicom general exception handling
面试题精选:神奇的斐波那契数列
随机推荐
基于 R 语言的判别分析介绍与实践 LDA和QDA
leetcode 20. Valid Parentheses 有效的括号(中等)
昇腾AI开发者创享日南京站!一起CANN机器狗+AI机械臂实现硬核智慧救援!燃爆现场~
ftplib+ tqdm upload and download progress bar
read stream special attention
NFS pays special attention to the problem of permissions
glibc 内存管理模型 释放 C库内存缓存
Flutter entry and advanced tour (6) Layout Widget
SQL Server查询优化 (转载非原创)
Flutter introduction advanced trip (5) Image Widget
eslint语法规则报错
JVM内存泄漏和内存溢出的原因
ArcEngine(八) 选择要素并高亮显示
5G China unicom repeater network management protocol real-time requirements
5G China unicom 一般性异常处理
一维数组&指针
GIN a preliminary study, the environment is installed
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
ARM板卡增加路由功能
用plot_hist_numeric()实现画直方图