
在 web 开发中,经常会遇到这样的场景:用户在一个 html 页面中输入了信息(例如填写表单),然后需要将这些信息传递到另一个 html 页面进行展示或进一步处理。原问题中提到的“如何在另一个 html 文件中使用一个 html 文件的类和 id”实际上是对这种数据传输需求的误解。html 元素的类和 id 是其内部 dom 元素的标识符,它们本身不能直接跨文件“使用”。真正的需求是实现不同页面间的数据共享。
本文将介绍一种常用且高效的客户端数据存储机制——localStorage,来解决这一问题。通过 localStorage,我们可以在浏览器中持久化存储数据,使其在用户关闭浏览器后依然存在,并且可以在同一域下的不同页面间共享。
localStorage 是 Web Storage API 的一部分,它允许 Web 应用程序在用户的浏览器中存储键值对。这些数据以字符串形式存储,并且在浏览器会话结束后仍然保留,除非用户手动清除或通过代码移除。其主要特点包括:
我们将通过两个 HTML 文件来演示这一过程:一个用于收集表单数据(源页面),另一个用于展示这些数据(目标页面)。
首先,我们需要创建一个包含表单的 HTML 页面,当用户提交表单时,我们捕获输入值,并将其存储到 localStorage 中。
立即学习“前端免费学习笔记(深入)”;
HTML 结构 (index.html)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生信息录入</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
form { background-color: #f4f4f4; padding: 20px; border-radius: 8px; max-width: 400px; margin: auto; }
label { display: block; margin-bottom: 5px; font-weight: bold; }
input[type="text"] { width: calc(100% - 22px); padding: 10px; margin-bottom: 15px; border: 1px solid #ccc; border-radius: 4px; }
button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
button:hover { background-color: #45a049; }
</style>
</head>
<body>
<form id="studentForm">
<h2>学生信息录入</h2>
<label for="studentName">学生姓名:</label>
<input type="text" id="studentName" placeholder="请输入学生姓名" required>
<label for="studentId">学号:</label>
<input type="text" id="studentId" placeholder="请输入学号" required>
<label for="studentClass">班级:</label>
<input type="text" id="studentClass" placeholder="请输入班级">
<button type="submit">提交信息</button>
</form>
<script>
// 获取表单元素
const form = document.getElementById("studentForm");
// 监听表单提交事件
form.addEventListener("submit", function(event) {
// 阻止表单默认提交行为,避免页面刷新
event.preventDefault();
// 获取输入框的值
const studentName = document.getElementById('studentName').value;
const studentId = document.getElementById('studentId').value;
const studentClass = document.getElementById('studentClass').value;
// 将获取到的数据封装成一个 JavaScript 对象
const studentData = {
name: studentName,
id: studentId,
class: studentClass
};
// 将 JavaScript 对象转换为 JSON 字符串并存储到 localStorage
// 注意:localStorage 只能存储字符串
localStorage.setItem("currentStudent", JSON.stringify(studentData));
// 提交成功后,可以给用户一个反馈,并跳转到目标页面
alert("学生信息已成功保存!");
window.location.href = "display.html"; // 跳转到展示页面
});
</script>
</body>
</html>代码解析:
接下来,我们创建另一个 HTML 页面 (display.html),用于从 localStorage 中读取之前存储的数据,并将其展示出来。
HTML 结构 (display.html)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生信息详情</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.container { background-color: #e6f7ff; padding: 20px; border-radius: 8px; max-width: 600px; margin: auto; border: 1px solid #91d5ff; }以上就是HTML 文件间数据共享教程:通过 localStorage 传递表单输入值的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号