全面剖析 <input> 标签 ------ HTML\HTML5

本文详细剖析了HTML5中<input>标签的定义、使用、说明、属性、标签类型及其特性,包括新属性、兼容性及不同类型如text、password、checkbox等的应用实例。此外,还介绍了HTML5新增的type类型,如email、url、number、range、search、color等,以及如何在实际项目中灵活运用。

<input>标签因其形式多样、功能强大,当之无愧成为了WEB前端开发人员最钟爱的元素之一。下面就来对<input>做一个全面的剖析:

标签定义:

<input> 标签用于搜集用户信息。

标签使用:

<input> 元素在 <form> 元素中使用,用来声明允许用户输入数据的 input 控件。(根据不同的 type 属性值,输入字段拥有很多种形式。)  

标签说明:

1)HTML 4.01 与 HTML5之间的差异:

    在 HTML 4.01 中, "align" 数据已经不再使用。HTML5 中不支持该属性。

    在 HTML5中, <input> 添加了几个属性,并且添加了对应的值。

2)HTML 与 XHTML之间的差异:

    在 HTML 中,<input> 标签没有结束标签。

    在 XHTML 中,<input> 标签必须被正确地关闭。

标签属性:

new :HTML5 中的新属性

 

属性

描述

accept

mime_type

规定通过文件上传来提交的文件的类型。 (只针对type="file")

align

left 

Right

top 

middle 

bottom

HTML5已废弃,不赞成使用。规定图像输入的对齐方式。 (只针对type="image")

alt

text

定义图像输入的替代文本。 (只针对type="image")

autocomplete     New

on 

off

规定输入字段是否应该启用自动完成功能。

autofocus         New

autofocus

规定输入字段在页面加载是否获得焦点。

checked

checked

规定在页面加载时应该被预先选定的 <input> 元素。 (只针对 type="checkbox" 或者 type="radio")

disabled

disabled

规定应该禁用的 <input> 元素。

form               New

formname

规定 <input> 元素所属的一个或多个表单。

formaction        New

URL

覆盖表单的 action 属性。(只针对 type="submit" 和 type="image")

formenctype       New

application/x-www-form-urlencoded multipart/form-data text/plain

覆盖表单的 enctype 属性。

(只适合 type="submit" 和 type="image")。

formmethod       New

get 

post

覆盖表单的 method属性。

(只适合 type="submit" 和 type="image")

formnovalidate   New

formnovalidate

覆盖表单的 novalidate 属性。

如果使用该属性,则提交表单时不进行验证。

formtarget       New

_blank

 _self

 _parent 

_top 

framename

覆盖表单的 target 属性。

(只适合 type="submit" 和 type="image")

height             New

pixels

定义 input 字段的高度。

(适用于 type="image")

list                      New

datalist_id

引用包含输入字段的预定义选项的 datalist 。

max               New

number 

date

规定输入字段的最大值。

maxlength

number

规定输入字段中的字符最大长度。

min               New

number 

date

规定输入字段的最小值。

multiple         New

multiple

规定允许用户输入多个值到 input元素。

name

Filed_name

规定 input 元素的名称。

pattern          New

Regexp_pattern

规定输入字段的值的模式或格式。

例如 pattern="[0-9]" 表示输入值必须是 0 与 9 之间的数字

placeholder      New

text

规定帮助用户填写输入字段的提示信息 。

readonly

readonly

规定输入字段是只读的。

required         New

required

规定输入字段的值为必需。

size

number

规定输入字段的宽度。

src

URL

规定显示为提交按钮的图像的 URL。 

(只针对 type="image")

step              New

Number_of_char

规定输入字的合法数字间隔。

type

见后详解

规定input元素的类型。

value

value

规定input元素的值。

width            New

pixels

规定 input 字段的宽度。 

(只针对type="image")

 

标签类型 (type):

说明:所有主流浏览器都支持 type 属性,但是并非所有主流浏览器都支持所有不同的 input 类型。

 

text:默认。定义一个单行的文本字段。(默认宽度为20)


password:定义密码字段。(字符会被遮盖)


checkbox:复选框(checked属性:是否选中)


radio:单选框(name属性:指定多个单选框的在一个区域里进行单选操作)

 

button:定义普通按钮。常用于与JS一起启动脚本。(value属性:button按钮显示的文本)


reset:定义重置按钮(清除文本区域内容,重置所有表单值为默认值)


submit:定义提交按钮。

 

hide:定义隐藏输入字段。(存放仅用于自己使用的信息)

 

file:定义输入字段和 "浏览"按钮,供文件上传。

 

image:定义图像形式的提交按钮。

 

以下类型为 HTML5 新增 type 类型:

 

email:在提交表单时会自动验证email的格式是否正确,格式不正确浏览器是不允许提交的。opera浏览器中必须有name属性,否则不起作用。

<input type="email" name="email">

 

url:在提交表单时会自动验证url的格式。Opera中会自动在开始处添加http://.

<input type="url" />

 

number:可以限制输入的数字,若未输入则会抛出一个错误。(step为上一个数字与下一个数字的间隔)

<input type="number" max="12" min="0" step="1"/>

 

range:此类型将显示一个可拖动的滑块条,能够选择性的对限制范围内的数值进行设置。拖动时会反馈给value一个值.

<input type="range"  min="1" max="10" />


search:此类型表示输入的将是一个搜索关键字。

<input type="search" />


color: 此类型表单可让选择颜色值,并反馈到value中.

<input type="color"/>

 

telephone:此类型可输入一个电话号码。

<input type="telephone" />

 

Date pickers (date, month, week, time, datetime, datetime-local):( 供选取日期和时间 )

date - 选取日、月、年
month - 选取月、年
week - 选取周和年
time - 选取时间(小时和分钟)
datetime - 选取时间、日、月、年(UTC 时间)
datetime-local - 选取时间、日、月、年(本地时间)

 

注:“HTML5新增属性的样例显示以及具体兼容性情况”可参考  

      http://mrthink.net/demo/ihtml520110220.htm     出处:Mr.Think博客

 

转载于:https://www.cnblogs.com/tianjuan/p/5258136.html

<!DOCTYPE html><br/><html lang="zh"><br/><head><br/> <meta charset="UTF-8" /><br/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/><br/> <title>征婚网注册</title><br/> <style><br/> body {<br/> font-family: "Microsoft YaHei", sans-serif;<br/> background-color: #f5f5f5;<br/> display: flex;<br/> justify-content: center;<br/> align-items: center;<br/> height: 100vh;<br/> margin: 0;<br/> }<br/> .form-container {<br/> width: 400px;<br/> padding: 30px;<br/> background: white;<br/> border-radius: 10px;<br/> box-shadow: 0 4px 10px rgba(0,0,0,0.1);<br/> }<br/> h2 {<br/> text-align: center;<br/> color: #d63384;<br/> }<br/> .input-group {<br/> margin-bottom: 15px;<br/> }<br/> label {<br/> display: block;<br/> margin-bottom: 5px;<br/> font-weight: bold;<br/> }<br/> input[type="text"], input[type="tel"], select {<br/> width: 100%;<br/> padding: 8px;<br/> border: 1px solid #ccc;<br/> border-radius: 5px;<br/> }<br/> .checkbox-group {<br/> display: flex;<br/> flex-wrap: wrap;<br/> gap: 10px;<br/> }<br/> .checkbox-item {<br/> display: flex;<br/> align-items: center;<br/> }<br/> .checkbox-item input {<br/> margin-right: 5px;<br/> }<br/> .btn-group {<br/> display: flex;<br/> justify-content: space-between;<br/> margin-top: 20px;<br/> }<br/> button {<br/> padding: 10px 20px;<br/> border: none;<br/> border-radius: 5px;<br/> cursor: pointer;<br/> }<br/> .reset-btn {<br/> background: #6c757d;<br/> color: white;<br/> }<br/> .login-btn {<br/> background: #d63384;<br/> color: white;<br/> }<br/> </style><br/></head><br/><body><br/><div class="form-container"><br/> <h2>征婚网</h2><br/> <div class="input-group"><br/> <label>性别:</label><br/> <div><br/> <input type="radio" id="male" name="gender" value="男" checked /><br/> <label for="male">男</label><br/> <input type="radio" id="female" name="gender" value="女" /><br/> <label for="female">女</label><br/> </div><br/> </div><br/> <div class="input-group"><br/> <label>出生年月:</label><br/> <select style="width: 32%; display: inline-block;"><br/> <option>请选择年</option><br/> <!-- 年份循环 --><br/> <script><br/> const year = new Date().getFullYear();<br/> for (let i = year; i >= 1950; i--) {<br/> document.currentScript.previousElementSibling.innerHTML += `<option>${i}</option>`;<br/> }<br/> </script><br/> </select><br/> <select style="width: 30%; display: inline-block;"><br/> <option>请选择月</option><br/> <script><br/> for (let i = 1; i <= 12; i++) {<br/> document.currentScript.previousElementSibling.innerHTML += `<option>${i}</option>`;<br/> }<br/> </script><br/> </select><br/> <select style="width: 30%; display: inline-block;"><br/> <option>请选择日</option><br/> <script><br/> for (let i = 1; i <= 31; i++) {<br/> document.currentScript.previousElementSibling.innerHTML += `<option>${i}</option>`;<br/> }<br/> </script><br/> </select><br/> </div><br/> <div class="input-group"><br/> <label>所在地区:</label><br/> <input type="text" value="奋斗者之家" readonly /><br/> </div><br/> <div class="input-group"><br/> <label>婚姻状况:</label><br/> <div><br/> <input type="radio" id="single" name="status" value="单身" checked /><br/> <label for="single">单身</label><br/> <input type="radio" id="unmarried" name="status" value="未婚" /><br/> <label for="unmarried">未婚</label><br/> <input type="radio" id="divorced" name="status" value="离异" /><br/> <label for="divorced">离异</label><br/> </div><br/> </div><br/> <div class="input-group"><br/> <label>学历:</label><br/> <input type="text" value="小学" readonly /><br/> </div><br/> <div class="input-group"><br/> <label>月薪:</label><br/> <input type="text" value="1000~100000" readonly /><br/> </div><br/> <div class="input-group"><br/> <label>手机号:</label><br/> <input type="tel" placeholder="请输入手机号" /><br/> </div><br/> <div class="input-group"><br/> <label>昵称:</label><br/> <input type="text" placeholder="请输入昵称" /><br/> </div><br/> <div class="input-group"><br/> <label>喜欢的类型:</label><br/> <div class="checkbox-group"><br/> <div class="checkbox-item"><input type="checkbox" /> <label>爱打篮球</label></div><br/> <div class="checkbox-item"><input type="checkbox" /> <label>温柔</label></div><br/> <div class="checkbox-item"><input type="checkbox" /> <label>可爱</label></div><br/> <div class="checkbox-item"><input type="checkbox" /> <label>小鲜肉</label></div><br/> <div class="checkbox-item"><input type="checkbox" /> <label>御姐</label></div><br/> <div class="checkbox-item"><input type="checkbox" /> <label>萝莉</label></div><br/> </div><br/> </div><br/> <div class="input-group"><br/> <label>自我介绍:</label><br/> <textarea rows="3" style="width:100%; padding:8px; border:1px solid #ccc; border-radius:5px;" placeholder="请填写自我介绍"></textarea><br/> </div><br/> <div class="input-group"><br/> <div><input type="checkbox" checked /> 我承诺年满18岁、单身</div><br/> <div><input type="checkbox" checked /> 抱着严肃的态度</div><br/> <div><input type="checkbox" checked /> 真诚寻找另一半</div><br/> <div><input type="checkbox" /> 我同意注册条款和会员加入标准</div><br/> </div><br/> <div class="btn-group"><br/> <button class="reset-btn">RESET</button><br/> <button class="login-btn">登录</button><br/> </div><br/></div><br/></body><br/></html>
最新发布
10-30
<form action="xxx.jsp" method="post" > <table width="500"> <style> body { display: grid; place-content: center; min-height: 100vh; margin: 20px; font-family: 'Microsoft YaHei', sans-serif; font-size: 16px; } table { border-spacing: 10px; } </style> <tr> <td><b style="font-size: 16px;">性别</b></td> <td> <input type="radio" id="man" name="sex"/> <label for="man"><img src="images/man.jpg"> 男 </label> <input type="radio" id="women" name="sex"/> <label for="women"><img src="images/women.jpg"> 女 </label> </td> </tr> <tr> <td><b>生日</b></td> <td> <select name="year"> <option selected="selected">--请选择年--</option> <option>1990</option> <option>2000</option> <option>2010</option> </select> <select name="month"> <option selected="selected">--请选择月--</option> <option>1</option> <option>2</option> <option>3</option> </select> <select name="day"> <option selected="selected">--请选择日--</option> <option>11</option> <option>12</option> <option>13</option> </select> </td> </tr> <tr> <td><b>所在地区</b></td> <td> <input type="text" value="安徽" name="area"> </td> </tr> <tr> <td><b>婚姻状况</b></td> <td> <input type="radio" name="marital_status" id="spinsterhood"> <label for="spinsterhood"> 未婚 </label> <input type="radio" name="marital_status" id="married"> <label for="married"> 已婚 </label> <input type="radio" name="marital_status" id="divorce"> <label for="divorce"> 离婚 </label> <input type="radio" name="marital_status" id="secret"> <label for="secret">保密</label> </td> </tr> <tr> <td><b>学历</b></td> <td><input type="text" name="edu_bg"></td> </tr> <tr> <td><b>喜欢的类型</b></td> <td> <input type="checkbox" name="like_type" id="wumei"> <label for="wumei"> 妩媚的 </label> <input type="checkbox" name="like_type" id="keai"> <label for="keai"> 可爱的 </label> <input type="checkbox" name="like_type" id="xiaoxianrou"> <label for="xiaoxianrou"> 小鲜肉 </label> <input type="checkbox" name="like_type" id="laolarou"> <label for="laolarou"> 老腊肉 </label> <input type="checkbox" name="like_type" id="douxihuan"> <label for="douxihuan"> 都喜欢 </label> </td> </tr> <tr> <td><b>自我介绍</b></td> <td> <textarea cols="30" rows="10" name="self_introduction"></textarea> </td> </tr> <tr> <td colspan="2" style="text-align: center;"> <input type="submit" value=" 免费注册 "> </td> </tr> </table> </fieldset> </form>
04-01
提取下面内容的所有中文<template> <a-card> <a-row type="flex" justify="space-between" style="margin-bottom: 10px;"> <a-radio-group v-model:value="detailType"> <a-radio-button value="contrast">{{ $t('community_contribution.contrast') }}</a-radio-button> <a-radio-button value="trend">{{ $t('community_contribution.trend') }}</a-radio-button> </a-radio-group> <span> <span class="tool-style"> <span>仓名:</span> <a-select style="width: 200px;"> </a-select> </span> <span v-if="detailType === 'contrast'" class="tool-style"> <span>{{ $t('community_contribution.dimensions') }}:</span> <a-radio-group v-model:value="orgType"> <a-radio-button value="year_on_year"> <span>{{ $t('community_contribution.year_on_year') }}</span> <a-select style="width: 120px; margin-left: 2px;" size="small" :bordered="false" placeholder="请选择年份"> </a-select> </a-radio-button> <a-radio-button value="month_on_month">{{ $t('community_contribution.month_on_month') }}</a-radio-button> </a-radio-group> </span> <span class="tool-style"> <span v-if="detailType === 'contrast'">区间:</span> <span v-else>{{ $t('community_contribution.creation_date') }}:</span> <a-radio-group v-model:value="timeType"> <a-radio-button value="week">{{ $t('global.week') }}</a-radio-button> <a-radio-button value="month">{{ $t('global.month') }}</a-radio-button> <a-radio-button value="year">{{ $t('global.year') }}</a-radio-button> </a-radio-group> </span> <span v-if="detailType === 'contrast'"> <span>基线日期:</span> <a-date-picker v-model:value="timeBase" ></a-date-picker> </span> <span v-else> <a-range-picker v-model:value="timeRange" ></a-range-picker> </span> </span> </a-row> <div v-if="detailType === 'contrast'"> <a-card size="small" title="项目活跃度度量" :head-style="{ backgroundColor: '#f2f7ff'}"> <a-row> <a-col v-for="item in pieData.active_metric" :key="item" :span="8"> <pieChart :id="item.id" :legend-names="item.legend_name" :title="item.title" :value="item.value"></pieChart> </a-col> </a-row> </a-card> <a-card style="margin-top: 10px;" size="small" title="项目影响力度量" :head-style="{ backgroundColor: '#f2f7ff'}"> <a-row> <a-col v-for="item in pieData.impact_measurement" :key="item" :span="8"> <pieChart :id="item.id" :legend-names="item.legend_name" :title="item.title" :value="item.value"></pieChart> </a-col> </a-row> </a-card> </div> <div v-if="detailType === 'trend'"> <a-card v-for="item in lineData" :key="item" size="small" :title="item.title" :head-style="{ backgroundColor: '#f2f7ff'}" style="margin-top: 10px;"> <lineChart :id="item.id" :title="item.title" :value="item.value"></lineChart> </a-card> </div> </a-card> </template> <script lang="ts" setup> import { onMounted, ref } from 'vue'; import i18n from '@/locales'; import pieChart from './components/pieChart.vue'; import lineChart from './components/lineChart.vue'; const detailType = ref<any>('contrast'); const orgType = ref<any>('year_on_year'); const timeType = ref<any>('week'); const timeRange = ref<any>([]); const timeBase = ref<any>(''); const pieRef = ref<any>(null); const LEGEND_NAME = ['本月', '上月']; const pieData = ref<any>({ active_metric: [ { id: 'chart_requests_merged', title: '合入PR数', legend_name: LEGEND_NAME, value: [], }, { id: 'chart_issues', title: 'Issue数', legend_name: LEGEND_NAME, value: [], }, { id: 'chart_comments', title: '评审意见数', legend_name: LEGEND_NAME, value: [], }, ], impact_measurement: [ { id: 'chart_watch', title: '仓Watch数', legend_name: LEGEND_NAME, value: [], }, { id: 'chart_star', title: '仓Star数', legend_name: LEGEND_NAME, value: [], }, { id: 'chart_fork', title: '仓Fork数', legend_name: LEGEND_NAME, value: [], }, ] }); const lineData = ref<any>([ { id: 'chart_pr_trend', title: '合入PR数趋势', value: { xData: [], yData: [], }, }, { id: 'chart_issue_trend', title: 'Issue数趋势', value: { xData: [], yData: [], }, }, { id: 'chart_comment_trend', title: '评审意见数趋势', value: { xData: [], yData: [], }, }, ]); </script> <style lang="less"> .tool-style { margin-right: 20px; } </style>
08-16
{% extends "base.html" %} {% block title %}系统管理{% endblock %} {% block content %} <div class="admin-panel"> <h1>管理控制台</h1> <!-- 权限验证 --> {% if not current_user.is_admin %} <div class="alert alert-danger"> 错误:您没有访问此页面的权限 </div> {% else %} <section class="admin-section"> <h2>用户管理</h2> <table class="data-table"> <thead> <tr> <th>ID</th> <th>用户名</th> <th>最后登录</th> </tr> </thead> <tbody> {% for user in user_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.username }}</td> <td>{{ user.last_login | datetimeformat }}</td> </tr> {% endfor %} </tbody> </table> </section> {% endif %} </div> {% endblock %}{% extends "base.html" %} {% block title %}用户登录{% endblock %} {% block content %} <form class="auth-form" method="POST" action="{{ url_for('login') }}"> <h2>用户登录</h2> <!-- CSRF保护 --> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"> <div class="form-group"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required pattern="[A-Za-z0-9]{3,20}" title="仅允许字母和数字 (3-20字符)"> </div> <div class="form-group"> <label for="password">密码:</label> <input type="password" id="password" name="password" required minlength="8" title="至少8位字符"> </div> <button type="submit" class="btn-primary">登录</button> <!-- 安全增强措施 --> <div class="security-tips"> <p>🔒 采用HTTPS加密传输</p > <p>⚠ 登录失败3次将锁定账户</p > </div> </form> {% endblock %}<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{% block title %}安全增强网站{% endblock %}</title> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> </head> <body> <header class="navbar"> <nav> 首页 {% if current_user.is_authenticated %} 登出 {% if current_user.is_admin %} 管理后台 {% endif %} {% else %} 登录 {% endif %} </nav> </header> <main class="container"> <!-- 安全警告显示区域 --> {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %} <div class="alert alert-{{ category }}"> {{ message }} </div> {% endfor %} {% endif %} {% endwith %} {% block content %}{% endblock %} </main> <script src="{{ url_for('static', filename='main.js') }}"></script> </body> </html>这三个代码和你写的相比真么样
03-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值