网络安全作业三


DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }
        .login-container {
            background-color: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 350px;
            transition: transform 0.2s ease;
        }
        .login-container:hover {
            transform: translateY(-3px);
        }
        .login-title {
            text-align: center;
            color: #1a73e8;
            margin-bottom: 1.5rem;
            padding-bottom: 0.8rem;
            border-bottom: 2px solid #1a73e8;
        }
        .form-group {
            margin-bottom: 1.2rem;
            position: relative;
        }
        label {
            display: block;
            margin-bottom: 0.5rem;
            color: #5f6368;
        }
        .input-wrapper {
            position: relative;
        }
        input {
            width: 100%;
            padding: 0.8rem 0.8rem 0.8rem 2.5rem;
            border: 1px solid #dadce0;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 1rem;
            transition: all 0.2s;
        }
        .input-icon {
            position: absolute;
            left: 0.8rem;
            top: 50%;
            transform: translateY(-50%);
            color: #86909C;
        }
        input:focus {
            outline: none;
            border-color: #1a73e8;
            box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
        }
        .login-btn {
            width: 100%;
            padding: 0.8rem;
            background-color: #1a73e8;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.2s;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 8px;
        }
        .login-btn:hover {
            background-color: #1557b0;
        }
        .login-btn.loading {
            background-color: #69b1ff;
            cursor: wait;
        }
        .spinner {
            display: none;
            width: 16px;
            height: 16px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 1s linear infinite;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        .login-btn.loading .spinner {
            display: inline-block;
        }
        .error-message {
            color: #d93025;
            text-align: center;
            margin: 1rem 0;
            padding: 0.6rem;
            background-color: #fff8f8;
            border-radius: 4px;
            display: none;
            border: 1px solid #ffebee;
        }
        .form-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 1rem 0;
            font-size: 0.9rem;
        }
        .remember-me {
            display: flex;
            align-items: center;
            gap: 5px;
            cursor: pointer;
        }
        .remember-me input {
            width: auto;
            padding: 0;
        }
        .forgot-link {
            color: #1a73e8;
            text-decoration: none;
        }
        .forgot-link:hover {
            text-decoration: underline;
        }
    style>
head>
<body>
    <div class="login-container">
        <h2 class="login-title">账户登录h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名label>
                <div class="input-wrapper">
                    <i class="fas fa-user input-icon">i>
                    <input type="text" id="username" name="username" required>
                div>
            div>
            <div class="form-group">
                <label for="password">密码label>
                <div class="input-wrapper">
                    <i class="fas fa-lock input-icon">i>
                    <input type="password" id="password" name="password" required>
                div>
            div>
            
            <div class="form-options">
                <label class="remember-me">
                    <input type="checkbox" id="rememberMe"> 记住我
                label>
                <a href="#" class="forgot-link">忘记密码?a>
            div>
            
            <button type="submit" class="login-btn">
                <span>登录span>
                <span class="spinner">span>
            button>
            <div class="error-message" id="errorMsg">用户名或密码错误div>
        form>
    div>

    <script>
        // 获取表单元素
        const loginForm = document.getElementById('loginForm');
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const rememberMe = document.getElementById('rememberMe');
        const errorMsg = document.getElementById('errorMsg');
        const loginBtn = document.querySelector('.login-btn');

        // 页面加载时检查记住的用户名
        document.addEventListener('DOMContentLoaded', () => {
            const savedUser = localStorage.getItem('savedUsername');
            if (savedUser) {
                usernameInput.value = savedUser;
                rememberMe.checked = true;
            }
        });

        // 表单提交事件
        loginForm.addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = usernameInput.value.trim();
            const password = passwordInput.value.trim();
            
            // 简单验证
            if (!username) {
                showError('请输入用户名');
                return;
            }
            if (!password) {
                showError('请输入密码');
                return;
            }
            
            // 显示加载状态
            loginBtn.classList.add('loading');
            loginBtn.disabled = true;
            hideError();
            
            try {
                // 模拟网络请求延迟
                await new Promise(resolve => setTimeout(resolve, 800));
                
                // 验证逻辑
                if (username === 'admin' && password === '123456') {
                    // 记住用户名
                    if (rememberMe.checked) {
                        localStorage.setItem('savedUsername', username);
                    } else {
                        localStorage.removeItem('savedUsername');
                    }
                    alert('登录成功!');
                    // 实际应用中使用: window.location.href = '首页地址';
                } else {
                    showError('用户名或密码错误');
                    passwordInput.value = '';
                }
            } finally {
                // 恢复按钮状态
                loginBtn.classList.remove('loading');
                loginBtn.disabled = false;
            }
        });

        // 错误提示控制
        function showError(message) {
            errorMsg.textContent = message;
            errorMsg.style.display = 'block';
            // 3秒后自动隐藏
            setTimeout(hideError, 3000);
        }

        function hideError() {
            errorMsg.style.display = 'none';
        }

        // 输入框获得焦点时隐藏错误信息
        [usernameInput, passwordInput].forEach(input => {
            input.addEventListener('focus', hideError);
        });
    script>
body>
html>

基础文档标签


  1. 声明文档类型为HTML5,告知浏览器使用HTML5标准解析页面,确保正确渲染新特性。


  2. HTML文档的根元素,lang="zh-CN"指定页面主要语言为简体中文,有助于对搜索引擎优化和辅助技术(如屏幕阅读器)有重要意义。


  3. 包含页面的元数据,这些信息不直接显示在页面上,但对浏览器和搜索引擎至关重要。


  4. 指定页面字符编码为UTF-8,支持包括中文在内的全球大多数语言字符,避免乱码问题。


  5. 用于响应式设计,width=device-width使页面宽度与设备屏幕宽度一致,initial-scale=1.0设置初始缩放比例为1,确保移动设备上显示正常。

  6. 用户登录
    定义页面标题,显示在浏览器标签页上,也用于书签和搜索引擎结果展示。


  7. 引入外部资源,这里用于加载Font Awesome图标库,提供表单中使用的用户图标(fa-user)和锁图标(fa-lock)。