当前位置: 首页 > news >正文

HTML<input>元素详解

HTML <input> 元素详解

<input> 是 HTML 表单中最核心的元素,用于创建各种交互式控件来接收用户输入。


一、基本语法

<input type="输入类型" name="字段名" value="默认值">

二、主要输入类型

1. 文本输入

<!-- 普通文本输入 -->
<input type="text" name="username" placeholder="请输入用户名"><!-- 密码输入 -->
<input type="password" name="password" placeholder="请输入密码"><!-- 多行文本 -->
<textarea name="bio" rows="4" cols="50"></textarea>

2. 选择输入

<!-- 单选按钮 -->
<input type="radio" name="gender" value="male" id="male">
<label for="male"></label>
<input type="radio" name="gender" value="female" id="female" checked>
<label for="female"></label><!-- 复选框 -->
<input type="checkbox" name="hobby" value="reading" id="reading">
<label for="reading">阅读</label>
<input type="checkbox" name="hobby" value="sports" id="sports" checked>
<label for="sports">运动</label><!-- 下拉选择 -->
<select name="country"><option value="">--请选择--</option><option value="cn">中国</option><option value="us" selected>美国</option>
</select>

3. 特殊输入类型

<!-- 数字输入 -->
<input type="number" name="age" min="18" max="99"><!-- 日期选择 -->
<input type="date" name="birthday"><!-- 颜色选择 -->
<input type="color" name="favcolor"><!-- 文件上传 -->
<input type="file" name="avatar" accept="image/*"><!-- 隐藏字段 -->
<input type="hidden" name="userid" value="12345">

三、重要属性

属性描述示例
name表单字段名称(必填)name="username"
value默认值value="默认文本"
placeholder输入提示placeholder="请输入..."
required必填字段required
readonly只读readonly
disabled禁用disabled
maxlength最大字符数maxlength="20"
min/max数值范围min="0" max="100"
step数值步长step="5"
pattern正则验证pattern="[A-Za-z]{3}"
autocomplete自动完成autocomplete="off"
autofocus自动聚焦autofocus

四、表单验证示例

<form><!-- 必填文本字段 --><input type="text" name="fullname" required placeholder="请输入全名"><!-- 邮箱验证 --><input type="email" name="email" required placeholder="请输入有效邮箱"><!-- 密码强度验证 --><input type="password" name="password" required minlength="8" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="必须包含大小写字母和数字"><!-- 自定义错误提示 --><input type="text" name="zipcode" pattern="\d{5}" oninvalid="this.setCustomValidity('请输入5位邮编')"oninput="this.setCustomValidity('')"><button type="submit">提交</button>
</form>

五、现代输入类型 (HTML5)

<!-- 搜索框 -->
<input type="search" name="q"><!-- 电话号码 -->
<input type="tel" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"><!-- URL输入 -->
<input type="url" name="website"><!-- 范围滑块 -->
<input type="range" name="volume" min="0" max="100" step="5"><!-- 日期时间 -->
<input type="datetime-local" name="meeting">

六、最佳实践

  1. 始终使用<label>:提高可访问性

    <label for="username">用户名:</label>
    <input type="text" id="username" name="username">
    
  2. 合理分组:使用<fieldset><legend>

    <fieldset><legend>个人信息</legend><!-- 表单字段 -->
    </fieldset>
    
  3. 移动端优化

    <!-- 调出数字键盘 -->
    <input type="tel" inputmode="numeric"><!-- 调出适合邮箱的键盘 -->
    <input type="email" inputmode="email">
    
  4. 安全考虑

    <!-- 防止自动填充 -->
    <input type="password" name="password" autocomplete="new-password"><!-- 防止XSS -->
    <input type="text" value="<?php echo htmlspecialchars($value); ?>">
    
  5. 样式一致性

    input, select, textarea {box-sizing: border-box;width: 100%;padding: 8px;margin: 5px 0;
    }
    
http://www.dtcms.com/a/263104.html

相关文章:

  • 《用奥卡姆剃刀原理,为前端开发“减负增效”》
  • 《微信生态裂变增长利器:推客小程序架构设计与商业落地》
  • python训练day45 Tensorborad使用介绍
  • Linux 日志监控工具对比:从 syslog 到 ELK 实战指南
  • 阶段二开始-第一章—8天Python从入门到精通【itheima】-121节+122节(函数和方法的类型注解+Union联合类型注解)
  • 【运维系列】【ubuntu22.04】安装GitLab
  • 2025年光学工程、精密仪器与光电子技术国际会议(OEPIOT 2025)
  • Armbian 25.5.1 Noble Gnome 开启远程桌面功能
  • 百度文心ERNIE 4.5 大模型系列正式开源
  • Windows 安装 nodejs npm
  • 数据生命周期管理实战:建、用、管、存、归档到销毁的全流程治理
  • 如何用废弃电脑变成服务器搭建web网站(公网访问零成本)
  • 24V转12V降压实际输出12.11V可行性分析
  • GitHub Actions配置python flake8和black
  • 云手机的用途都有哪些?
  • 51c大模型~合集144
  • 赋能低压分布式光伏“四可”建设,筑牢电网安全新防线
  • Java垃圾回收机制和三色标记算法
  • MySQL EXPLAIN 关键字详解
  • python学习打卡day58
  • 使用 C++ 和 OpenCV 构建驾驶员疲劳检测软件
  • Java设计模式之结构型模式(外观模式)介绍与说明
  • jenkins集成sonarqube(使用token进行远程调用)
  • 使用Python进行数据库交互:从SQL查询到ORM操作的安全实践指南
  • 【王阳明代数讲义】二十四史语料库与意气实体过程学说导引
  • 大学专业科普 | 云计算、大数据
  • 将 h264+g711a存为 mp4文件,记录
  • FreePDFv3.0.0:颠覆你的文献阅读习惯
  • 【RTSP从零实践】3、实现最简单的传输H264的RTSP服务器
  • GORM 高级查询实战:从性能优化到业务场景解决方案