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表单的基本结构和样式设计。

相关推荐
大家的林语冰1 天前
ES5 凉凉,Babel 8 正式发布,默认不再编译为 ES5 和 CJS......
前端·javascript·前端工程化
weedsfly1 天前
异步编程全景与事件循环——彻底搞懂 JS 执行机制
前端·javascript
用户059540174461 天前
AI Agent记忆测试踩坑实录:Mock骗了我一周,Mem0+pytest一招破局
前端·css
用户1733598075371 天前
纯前端 PDF 数字签名实战:Vue 3 + pdf-lib 在浏览器里完成签名嵌入
前端·javascript
JieE2121 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2121 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
kyriewen2 天前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
Larcher2 天前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
默_笙2 天前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别
javascript
jump_jump2 天前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染
javascript·性能优化·前端工程化