用户验证案列

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            display: inline-block;
            width: 250px;
            height: 30px;
            vertical-align: middle;
            line-height: 30px;
            padding-left: 15px;
        }

        .error {
            color: red;
            background: url(./images/error1.png) no-repeat left center;
        }

        .right {
            color: green;
            background: url(./images/right.png) no-repeat left center;
        }
    </style>
</head>

<body>
    <input type="text">
    <span></span>
    <script>
        // 规定正则表达式
        const reg = /^[a-zA-Z0-9-_]{6,16}$/
        // 获取元素
        const input = document.querySelector('input')
        const span = input.nextElementSibling
        // 当表单失去焦点时开始验证
        input.addEventListener('blur', function () {
            if (reg.test(input.value)) {
                span.innerHTML = '输入正确'
                span.className = 'right'
            } else {
                span.innerHTML = '请输入6-16位的字母数字或-_'
                span.className = 'error'

            }
        })
    </script>
</body>

</html>

### HTML 实战案例与项目经验 HTML 是构建网页的基础语言,其应用范围广泛,涵盖了静态页面开发、动态交互设计等多个领域。以下是几个典型的 HTML 实战案例及其对应的项目经验和示例代码。 #### 1. 动态条件渲染 在前端开发中,动态显示隐藏内容是一个常见的需求。可以利用 JavaScript 和 HTML 结合的方式实现这一功能。例如,在 Vue.js 中可以通过 `v-if` 或 `v-show` 指令完成动态渲染[^1]: ```html <div id="app"> <div v-if="Math.random() > 0.5">Now you see me</div> <div v-else>Now you don't</div> </div> <script src="https://cdn.jsdelivr.net/npm/vue@2"></script> <script> new Vue({ el: '#app' }); </script> ``` 此代码展示了如何基于随机数判断是否展示特定的内容。这种技术适用于需要根据数据状态切换视图的场景。 #### 2. 表单验证 表单验证是 Web 应用中的重要环节之一。通过纯 HTML 属性即可实现简单的客户端校验,无需额外引入脚本文件。以下是一个带有必填项和正则表达式匹配的表单示例: ```html <form action="/submit" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required pattern="[A-Za-z]{3,}" title="至少三个字母字符"><br><br> <label for="email">邮箱地址:</label> <input type="email" id="email" name="email" required><br><br> <button type="submit">提交</button> </form> ``` 在此例子中,`required` 属性确保字段不能为空;而 `pattern` 属性允许定义自定义规则用于输入过滤。 #### 3. 响应式布局 随着移动设备普及率提高,响应式设计成为不可或缺的一部分。借助 CSS Flexbox 或 Grid Layout 可轻松创建适应不同屏幕尺寸的设计方案。下面提供了一个基础网格系统的实现方法: ```css .container { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); } .item { background-color: lightblue; padding: 1em; text-align: center; } ``` ```html <div class="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> <!-- 更多 item --> </div> ``` 这段代码演示了如何使用 CSS Grid 创建自动调整列宽的容器布局,从而支持多种分辨率下的良好用户体验。 #### 4. 单页应用程序 (SPA) 现代 Web 开发趋势倾向于采用 SPA 架构以改善加载速度并增强互动体验。虽然这通常涉及框架如 React 或 Angular 的运用,但也可以仅依靠原生 HTML/JavaScript 完成基本版本的功能模拟。这里给出一个简易导航菜单的例子: ```javascript document.querySelectorAll('.nav-link').forEach(link => { link.addEventListener('click', event => { event.preventDefault(); const targetId = event.target.getAttribute('href'); document.querySelector(targetId).scrollIntoView({ behavior: 'smooth' }); }); }); ``` ```html <nav> <a href="#section1" class="nav-link">Section 1</a> <a href="#section2" class="nav-link">Section 2</a> </nav> <section id="section1"> <h2>Section One Content</h2> </section> <section id="section2"> <h2>Section Two Content</h2> </section> ``` 以上片段实现了平滑滚动至指定锚点位置的效果,适合小型网站内部链接跳转用途[^1]。 --- ### 总结 无论是简单还是复杂的 HTML 项目,良好的代码组织能力和清晰的逻辑思维都是成功的关键因素。同时也要注意视觉呈现的质量,因为这是直接影响用户感受的重要方面[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值