html生成注册与登录代码

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

html 复制代码
<!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表单的基本结构和样式设计。

相关推荐
品克缤6 小时前
Element UI MessageBox 增加第三个按钮(DOM Hack 方案)
前端·javascript·vue.js
小沐°6 小时前
vue-设置不同环境的打包和运行
前端·javascript·vue.js
qq_419854057 小时前
CSS动效
前端·javascript·css
南村群童欺我老无力.7 小时前
Flutter应用鸿蒙迁移实战:性能优化与渐进式迁移指南
javascript·flutter·ci/cd·华为·性能优化·typescript·harmonyos
花哥码天下8 小时前
恢复网站console.log的脚本
前端·javascript·vue.js
奔跑的呱呱牛8 小时前
geojson-to-wkt 坐标格式转换
javascript·arcgis
康一夏9 小时前
React面试题,封装useEffect
前端·javascript·react.js
❆VE❆10 小时前
WebSocket与SSE深度对比:技术差异、场景选型及一些疑惑
前端·javascript·网络·websocket·网络协议·sse
ConardLi10 小时前
SFT、RAG 调优效率翻倍!垂直领域大模型评估实战指南
前端·javascript·后端
over69711 小时前
🌟 JavaScript 数组终极指南:从零基础到工程级实战
前端·javascript·前端框架