JavaScript 登录注册页面模板是一种常见的前端开发任务,用于创建用户认证系统的基本界面。以下是一个简单的示例,展示了如何使用HTML、CSS和JavaScript来构建一个基本的登录注册页面模板。
HTML: 用于构建页面的结构。 CSS: 用于美化页面的外观。 JavaScript: 用于添加交互性和处理用户输入。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login and Register</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div id="login-form">
<h2>Login</h2>
<form id="loginForm">
<input type="text" id="loginUsername" placeholder="Username" required>
<input type="password" id="loginPassword" placeholder="Password" required>
<button type="submit">Login</button>
</form>
</div>
<div id="register-form">
<h2>Register</h2>
<form id="registerForm">
<input type="text" id="registerUsername" placeholder="Username" required>
<input type="email" id="registerEmail" placeholder="Email" required>
<input type="password" id="registerPassword" placeholder="Password" required>
<button type="submit">Register</button>
</form>
</div>
</div>
<script src="script.js"></script>
</body>
</html>body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
form {
display: flex;
flex-direction: column;
}
input {
margin: 10px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('loginUsername').value;
const password = document.getElementById('loginPassword').value;
// 这里可以添加登录逻辑,例如发送请求到服务器验证用户信息
console.log('Login:', username, password);
});
document.getElementById('registerForm').addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('registerUsername').value;
const email = document.getElementById('registerEmail').value;
const password = document.getElementById('registerPassword').value;
// 这里可以添加注册逻辑,例如发送请求到服务器保存用户信息
console.log('Register:', username, email, password);
});问题: 表单提交后页面刷新。
问题: 用户输入验证不足。
通过以上代码和解释,你应该能够创建一个基本的登录注册页面,并理解其背后的原理和实现方式。
没有搜到相关的文章