html生成注册与登录代码

发布于:2025-02-11 ⋅ 阅读:(101) ⋅ 点赞:(0)

以下是一个简单的HTML代码示例,用于生成注册和登录表单。这个示例使用了基本的HTML和CSS来创建两个表单:一个用于注册,另一个用于登录。

<!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>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }
        h2 {
            margin-bottom: 20px;
        }
        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #218838;
        }
        .switch-form {
            margin-top: 20px;
        }
        .switch-form a {
            color: #007bff;
            text-decoration: none;
        }
        .switch-form a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>

<div class="container" id="login-container">
    <h2>登录</h2>
    <form id="login-form">
        <input type="text" placeholder="用户名" required>
        <input type="password" placeholder="密码" required>
        <input type="submit" value="登录">
    </form>
    <div class="switch-form">
        <p>还没有账号? <a href="#" onclick="switchToRegister()">注册</a></p>
    </div>
</div>

<div class="container" id="register-container" style="display: none;">
    <h2>注册</h2>
    <form id="register-form">
        <input type="text" placeholder="用户名" required>
        <input type="password" placeholder="密码" required>
        <input type="password" placeholder="确认密码" required>
        <input type="submit" value="注册">
    </form>
    <div class="switch-form">
        <p>已有账号? <a href="#" onclick="switchToLogin()">登录</a></p>
    </div>
</div>

<script>
    function switchToRegister() {
        document.getElementById('login-container').style.display = 'none';
        document.getElementById('register-container').style.display = 'block';
    }

    function switchToLogin() {
        document.getElementById('register-container').style.display = 'none';
        document.getElementById('login-container').style.display = 'block';
    }
</script>

</body>
</html>

代码说明:

  1. HTML结构

    • 有两个容器 #login-container 和 #register-container,分别用于登录和注册表单。

    • 每个表单包含输入字段(用户名、密码等)和一个提交按钮。

  2. CSS样式

    • 使用了一些基本的样式来美化表单,使其居中显示,并添加了一些阴影和圆角效果。

    • 输入字段和按钮的样式也进行了简单的设计。

  3. JavaScript功能

    • 使用 switchToRegister() 和 switchToLogin() 函数来切换显示登录和注册表单。

    • 通过 onclick 事件调用这些函数来切换表单的显示状态。

使用方法:

  • 将此代码保存为一个 .html 文件,然后在浏览器中打开即可看到效果。

  • 你可以根据需要进一步扩展和定制这个表单,例如添加表单验证、后端处理等。

这个示例是一个基础的实现,适合初学者理解HTML表单的基本结构和样式设计。


网站公告

今日签到

点亮在社区的每一天
去签到