loginForm.querySelector()和document.querySelector实现登录成功后能跳转到 index.html 页面

时间: 2023-10-12 21:20:47 浏览: 42
要实现登录成功后跳转到index.html页面,可以使用以下代码: ``` javascript const loginForm = document.querySelector('#login-form'); // 监听登录表单提交事件 loginForm.addEventListener('submit', (event) => { event.preventDefault(); // 阻止表单默认提交行为 // 获取表单数据 const username = loginForm.querySelector('#username').value.trim(); const password = loginForm.querySelector('#password').value.trim(); // 根据表单数据进行登录验证,并根据结果进行页面跳转 if (username === 'admin' && password === '123456') { window.location.href = 'index.html'; // 跳转到index.html页面 } else { alert('用户名或密码错误,请重新输入!'); } }); ``` 这里的关键是使用`window.location.href`属性实现页面跳转,`window.location.href`属性是一个可读写的属性,用于获取或设置当前页面的 URL 地址。将其设置为目标页面的 URL 地址即可跳转到目标页面。
相关问题

用html代码写一个可以相互跳转的登录和注册页面

<!DOCTYPE html> <html> <head> <title>Login and Register</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; } .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } form { display: flex; flex-direction: column; align-items: center; padding: 20px; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } input[type="text"], input[type="password"], input[type="email"] { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ccc; border-radius: 5px; font-size: 16px; box-sizing: border-box; } button { background-color: #4CAF50; color: white; padding: 10px; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; } button:hover { background-color: #3e8e41; } .link { margin-top: 20px; font-size: 16px; text-align: center; } .link a { color: #4CAF50; text-decoration: none; } .link a:hover { text-decoration: underline; } </style> </head> <body> <div class="container"> <form id="loginForm"> <h2>Login</h2> <label for="username">Username</label> <input type="text" id="username" name="username" required> <label for="password">Password</label> <input type="password" id="password" name="password" required> <button type="submit">Login</button> <div class="link"> Don't have an account? <a href="#registerForm">Register now</a> </div> </form> <form id="registerForm" style="display: none;"> <h2>Register</h2> <label for="email">Email</label> <input type="email" id="email" name="email" required> <label for="newUsername">Username</label> <input type="text" id="newUsername" name="newUsername" required> <label for="newPassword">Password</label> <input type="password" id="newPassword" name="newPassword" required> <button type="submit">Register</button> <div class="link"> Already have an account? <a href="#loginForm">Login now</a> </div> </form> </div> <script> // switch between login and register forms document.querySelectorAll('.link a').forEach(link => { link.addEventListener('click', e => { e.preventDefault(); document.querySelector(link.getAttribute('href')).style.display = 'flex'; document.querySelector(link.getAttribute('href') === '#loginForm' ? '#registerForm' : '#loginForm').style.display = 'none'; }); }); </script> </body> </html>

web前段开发 登录界面输入账号密码后跳转到对应页面的相关代码 大二简单一点的相关代码

以下是一个简单的登录界面的示例代码,使用了HTML、CSS和JavaScript: HTML代码: ``` <form id="login-form"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br> <input type="submit" value="Login"> </form> ``` CSS代码: ``` #login-form { margin: 20px; padding: 20px; border: 1px solid #ccc; border-radius: 5px; } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="password"] { padding: 5px; border-radius: 5px; border: 1px solid #ccc; margin-bottom: 10px; } input[type="submit"] { padding: 5px 10px; background-color: #4CAF50; color: #fff; border: none; border-radius: 5px; cursor: pointer; } ``` JavaScript代码: ``` const loginForm = document.querySelector('#login-form'); const usernameInput = document.querySelector('#username'); const passwordInput = document.querySelector('#password'); loginForm.addEventListener('submit', (event) => { event.preventDefault(); // 阻止表单提交 const username = usernameInput.value; const password = passwordInput.value; if (username === 'admin' && password === '123456') { window.location.href = 'home.html'; // 跳转到对应页面 } else { alert('Invalid username or password.'); // 显示错误提示 } }); ``` 这段代码会在用户点击登录按钮时检查输入的用户名和密码是否正确,如果正确则跳转到home.html页面,否则会显示错误提示。注意,这只是一个简单的示例,实际应用中需要进行更完善的登录验证和安全措施。

相关推荐

最新推荐

recommend-type

ADSP-BF533的例程源码29:FIR信号处理源码

ADSP-BF533的例程源码29:FIR信号处理源码
recommend-type

风险管理模板.png

风险管理模板.png
recommend-type

长玻纤增强聚丙烯市场:规模增长、竞争格局与未来机遇.pdf

QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。 邮箱:market@qyresearch.com
recommend-type

2024-“她经济”洞察报告-QuestMobile-41页

2024-“她经济”洞察报告-QuestMobile-41页
recommend-type

执法记录仪与采集站对接文档

公安部对执法记录仪的采集制定了标准接口,方便各开发方案商开发的执法记录仪可以在采集站上使用
recommend-type

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节 线性代数的核心问题是求解方程组。这些方程都是线性的,即未知数仅与数相乘——我们绝不会 遇见 x 乘以 y。我们的第一个线性方程组较小。接下来你来看看它引申出多远: 两个方程 两个未知数 x − 2y = 1 3x + 2y = 11 (1) 我们一次从一个行开始。第一个方程 x − 2y = 1 得出了 xy 平面的一条直线。由于点 x = 1, y = 0 解 出该方程,因此它在这条直线上。因为 3 − 2 = 1,所以点 x = 3, y = 1 也在这条直线上。若我们选择 x = 101,那我们求出 y = 50。 这条特定直线的斜率是 12,是因为当 x 变化 2 时 y 增加 1。斜率在微积分中很重要,然而这是线 性代数! 图 2.1 将展示第一条直线 x − 2y = 1。此“行图”中的第二条直线来自第二个方程 3x + 2y = 11。你 不能错过两条线的交点 x = 3, y = 1。点 (3, 1) 位于两条线上并且解出两个方程。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

识别MATLAB微分方程求解中的混沌行为:分析非线性方程混沌行为的实用技巧

![matlab求解微分方程](https://img-blog.csdnimg.cn/2021062810300367.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTQ3OTY3OA==,size_16,color_FFFFFF,t_70) # 1. MATLAB微分方程求解概述 微分方程是描述物理、工程和金融等领域中动态系统的数学模型。MATLAB提供了强大的工具来求解微分方程,包括内置求解器和自定义函数
recommend-type

physon如何做ERP系统

作为一个AI语言模型,我可以给您提供一些基本的建议和思路: 1.明确需求和目标:在开始开发ERP系统之前,需要明确您的业务需求和目标,了解您的业务流程、流程数据、员工角色和权限、财务管理等方面的要求,这样才能更好的设计和开发ERP系统。 2.选择技术框架和开发工具:选择合适的技术框架和开发工具是ERP系统开发的关键。选择一种流行的技术框架和工具可以提高开发效率和质量。 3.设计数据库:ERP系统需要一个功能强大的数据库来存储数据。设计数据库需要考虑数据的完整性、安全性和可扩展性。 4.设计系统架构:系统架构是ERP系统的骨架,需要考虑系统的可扩展性、可维护性和性能。 5.开发和测试:
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。