当前位置:网站首页>实现两个页面之前的通信(使用localStorage)
实现两个页面之前的通信(使用localStorage)
2022-07-24 03:05:00 【墨鱼爱吃圣代】
实现两个页面之前的通信(使用localStorage)
分析:
在text.html 里面添加一个输入框 input 作为传输的数据页面 添加一个存储按钮 button id = “btn” 存储输入框的数据
添加一个跳转链接 a
在text2.html 里面添加一个输入框 用于显示 从第一个页面接收到的数据
在text.js里面操作text.html
(1) 获取输入框 let input = document.querySelector(“#lq”);
(2) 通过 input.value = ‘lq’ 赋值可以实时进行更改 text.html 输入框内容
(3) 获取存储按钮 let btn = document.querySelector(“#btn”);
(4) 对按钮添加点击事件 点击按钮进行存储数据 localStorage.setItem(“msg”, result);在text2.js里面操作text2.html
(1) 获取数据 let result = localStorage.getItem(‘msg’);
(2) 获取text2.html输入框
(3) 将获取的值赋值给输入框
实现效果:
文件

左边是未给输入框赋值,右边是赋值了

点击存储按钮之后 右键检查 可以看到存储的数据,如果没有显示正确的输入框信息,可以刷新一下(绿色框)

点击跳转页面 页面会显示前一个页面的数据

在第一个页面更改数据,存储之后,跳转,第二个页面会显示修改之后的数据

代码:
text.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="lq" >
<button id="btn">存储</button>
<!-- <input type="text" id="repeat"> -->
<a href="./text2.html">跳转</a>
<script src="text.js"></script>
</body>
</html>
text.js
let input = document.querySelector("#lq"); // 获取input节点
input.value = 'lq';
let result = input.value;
let btn = document.querySelector("#btn");
btn.onclick = function() {
// console.log(input.value);
// let input2 = document.querySelector("#repeat");
// input2.value = input.value;
result = input.value;
localStorage.setItem("msg", result); // 存储 "msg"类似于属性名, result类似于 属性值
}
text2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="lq">
<script src="./text2.js"></script>
</body>
</html>
text2.js
let result = localStorage.getItem('msg'); // 用msg属性名 获取属性值
console.log(result);
let input = document.querySelector("#lq");
input.value = result;
边栏推荐
- Description of relevant resolutions in video on demand
- Summernote supports custom video upload function
- The first edition of Niuke brush question series (automorphic number, return the number of prime numbers less than N, and the first character only appears once)
- Diversity of SIGIR '22 recommendation system papers
- go log包
- In the future, when the interviewer asks why you don't recommend using select *, please answer him loudly!
- Customize the default width and height of kindeditor rich text
- 在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
- CMT 注册——Google Scholar Id,Semantic Scholar Id,和 DBLP Id
- C language exercises
猜你喜欢

SSM family financial management personal financial management system accounting system

CMT registration - Google Scholar ID, semantic scholar ID, and DBLP ID

TCP data transmission and performance
![Hospital PACS source code PACS ultrasonic Department source code DICOM image workstation source code [source code free sharing]](/img/7d/c309b7b0b919df44b5c1461d79e7e7.png)
Hospital PACS source code PACS ultrasonic Department source code DICOM image workstation source code [source code free sharing]

SolidWorks cannot reference references

O3DE 的Lumberyard 游戏引擎

Attack and defense world web practice area (view_source, get_post, robots)

Acwing 4498. pointer (DFS)

Ways to improve the utilization of openeuler resources 01: Introduction

Ugui source code analysis - imaskable
随机推荐
The solution of using non root user management in secure stand-alone database under general machine environment
Soft test --- fundamentals of programming language (Part 1)
Openresty Lua resty balancer dynamic load balancing
Unity Message push
summernote 字体显示中文
Do securities companies really have principal guaranteed financial products?
SSM's technical forum includes front and back offices
kettle
Jparepository extension interface
OSPF routing control
TCP data transmission and performance
The next stop of data visualization platform | gifts from domestic open source data visualization datart "super iron powder"
508. The subtree element with the most occurrences and the pure C implementation of hash table method
Basic knowledge of trigger (Part 2)
Is the reverse repurchase of treasury bonds safe? How to open an account online?
The simple use of ADB command combined with monkey is super detailed
kettle
198. House raiding
String.split()最详细源码解读及注意事项
关于Aries框架增删改查-查Demo