当前位置:网站首页>实现两个页面之前的通信(使用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;
边栏推荐
- 攻防世界WEB练习区(weak_auth、simple_php、xff_referer)
- Unscramble the category and application principle of robot vision
- Mobile keyboard (day 73)
- The function of SIP account - tell you what is SIP line
- 攻防世界WEB练习区(webshell、command_execution、simple_js)
- What is the security of Treasury reverse repo
- 在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
- Unity Message push
- Some properties of differential array operation
- Description of relevant resolutions in video on demand
猜你喜欢

A simple and perfect WPF management system framework source code
![[management / upgrade] * 02. View the upgrade path * FortiGate firewall](/img/c7/da6db46d372e7462cd14852b662d6d.png)
[management / upgrade] * 02. View the upgrade path * FortiGate firewall

O3DE 的Lumberyard 游戏引擎

Hcip --- BGP comprehensive experiment

FTP服務與配置

ssm的求职招聘系统兼职应聘求职

Lcd1602——斌哥51

Ugui source code analysis - maskablegraphic

I developed an app similar to wechat runnable applet with fluent

自定义kindeditor富文本默认的宽高
随机推荐
PMP first-hand data and information acquisition
Ugui source code analysis - stencilmaterial
Go IO operation - file write
kettle
Tweenmax+svg Pikachu transformation ball
[brother hero July training] day 23: dictionary tree
Binary tree traversal (day 74)
Mobile keyboard (day 73)
Summernote rich text editor
攻防世界WEB练习区(backup、cookie、disabled_button)
Binary tree traversal
X Actual combat - Cloud Server
JVM initial
The practical influence of educational robot on students' learning effect
Type de symbole
To forge ahead on a new journey, the city chain science and technology carnival was grandly held in Xiamen
微信公众号在线客服接入发方法和功能详解
Summernote supports custom video upload function
Soft test --- fundamentals of programming language (Part 1)
AcWing 4499. 画圆 (相似三角形)