JavaScript用户登录界面模板是一种常见的前端开发任务,用于创建用户可以输入用户名和密码以访问应用程序或网站的界面。以下是一个简单的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 Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form id="loginForm">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit">Login</button>
</form>
<div id="error-message" class="error"></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;
}
.login-container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
button {
width: 100%;
padding: 10px;
background-color: #28a745;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
.error {
color: red;
margin-top: 10px;
}document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
// 这里可以添加实际的登录验证逻辑,例如通过AJAX请求与服务器通信
if (username === 'admin' && password === 'password') {
alert('Login successful!');
// 重定向到另一个页面或执行其他操作
} else {
document.getElementById('error-message').textContent = 'Invalid username or password.';
}
});通过上述模板和解释,您可以创建一个基本的用户登录界面,并了解其背后的基础概念和技术细节。
没有搜到相关的沙龙