<!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>* {margin: 0;padding: 0;box-sizing: border-box;}body {font-family: Arial, sans-serif;background-color: #f5f5f5;display: flex;justify-content: center;align-items: center;height: 100vh;}.login-container {background-color: white;padding: 40px;border-radius: 8px;box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);width: 100%;max-width: 400px;}h2 {text-align: center;color: #333;margin-bottom: 30px;}.form-group {margin-bottom: 20px;}label {display: block;margin-bottom: 5px;color: #666;}input {width: 100%;padding: 10px;border: 1px solid #ddd;border-radius: 4px;font-size: 16px;}input:focus {outline: none;border-color: #4CAF50;}.error-message {color: #f44336;font-size: 14px;margin-top: 5px;display: none;}.login-btn {width: 100%;padding: 12px;background-color: #4CAF50;color: white;border: none;border-radius: 4px;font-size: 16px;cursor: pointer;transition: background-color 0.3s;}.login-btn:hover {background-color: #45a049;}.login-btn:active {background-color: #3d8b40;}</style>
</head>
<body><div class="login-container"><h2>用戶登錄</h2><form id="loginForm"><div class="form-group"><label for="username">用戶名</label><input type="text" id="username" name="username" placeholder="請輸入用戶名"><div class="error-message" id="usernameError">請輸入用戶名</div></div><div class="form-group"><label for="password">密碼</label><input type="password" id="password" name="password" placeholder="請輸入密碼"><div class="error-message" id="passwordError">請輸入密碼</div></div><button type="submit" class="login-btn">登錄</button></form></div><script>// 模擬的用戶數據const mockUsers = [{ username: 'admin', password: '123456' },{ username: 'user', password: 'password' }];// 獲取表單和輸入元素const loginForm = document.getElementById('loginForm');const usernameInput = document.getElementById('username');const passwordInput = document.getElementById('password');const usernameError = document.getElementById('usernameError');const passwordError = document.getElementById('passwordError');// 表單提交事件loginForm.addEventListener('submit', function(e) {e.preventDefault();// 重置錯誤信息usernameError.style.display = 'none';passwordError.style.display = 'none';// 獲取輸入值const username = usernameInput.value.trim();const password = passwordInput.value.trim();// 驗證輸入let isValid = true;if (username === '') {usernameError.style.display = 'block';isValid = false;}if (password === '') {passwordError.style.display = 'block';isValid = false;}if (isValid) {// 驗證用戶const user = mockUsers.find(u => u.username === username && u.password === password);if (user) {alert('登錄成功!歡迎 ' + username);// 這里可以重定向到主頁或其他頁面// window.location.href = 'home.html';} else {alert('用戶名或密碼錯誤!');}}});// 輸入時隱藏錯誤信息usernameInput.addEventListener('input', function() {usernameError.style.display = 'none';});passwordInput.addEventListener('input', function() {passwordError.style.display = 'none';});</script>
</body>
</html>