HTML5 Input 类型
更新时间:2021-11-19 07:09HTML5 拥有多个新的表单输入类型。这些新特性提供了更好的输入控制和验证。本章全面介绍这些新的输入类型。
- color
- date
- datetime
- datetime-local
- month
- number
- range
- search
- tel
- time
- url
- week
注意:并不是所有的主流浏览器都支持新的input类型,不过您已经可以在所有主流的浏览器中使用它们了。即使不被支持,仍然可以显示为常规的文本域。
Input 类型: color
color 类型用在input字段主要用于选取颜色,如下所示:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form> 选择你喜欢的颜色: <input type="color" name="favcolor"><br> </form> </body> </html>
Input 类型: date
date 类型允许你从一个日期选择器选择一个日期。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form > 生日: <input type="date" name="bday"> </form> </body> </html>
Input 类型: datetime-local
datetime-local 类型允许你选择一个日期和时间 (无时区).
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form > 生日 (日期和时间): <input type="datetime-local" name="bdaytime"> </form> </body> </html>
Input 类型: email
email 类型用于应该包含 e-mail 地址的输入域。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> E-mail: <input type="email" name="usremail"> <input type="submit"> </form> <p><b>注意:</b> Internet Explorer 9 及更早 IE 版本不支持 type="email" 。</p> </body> </html>
提示: iPhone 中的 Safari 浏览器支持 email 输入类型,并通过改变触摸屏键盘来配合它(添加 @ 和 .com 选项)
Input 类型: month
month 类型允许你选择一个月份。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> 生日 ( 月和年 ): <input type="month" name="bdaymonth"> <input type="submit"> </form> </body> </html>
Input 类型: number
number 类型用于应该包含数值的输入域。你还能够设定对所接受的数字的限定.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> 数量 ( 1 到 5 之间): <input type="number" name="quantity" min="1" max="5"> <input type="submit"> </form> <p><b>注意:</b>Internet Explorer 9 及更早 IE 版本不支持 type="number" 。</p> </body> </html>
使用下面的属性来规定对数字类型的限定:
属性 | 描述 |
---|---|
disabled | 规定输入字段是禁用的 |
max | 规定允许的最大值 |
maxlength | 规定输入字段的最大字符长度 |
min | 规定允许的最小值 |
pattern | 规定用于验证输入字段的模式 |
readonly | 规定输入字段的值无法修改 |
required | 规定输入字段的值是必需的 |
size | 规定输入字段中的可见字符数 |
step | 规定输入字段的合法数字间隔 |
value | 规定输入字段的默认值 |
Input 类型: range
range 类型用于应该包含一定范围内数字值的输入域。range 类型显示为滑动条。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action="" method="get"> Points: <input type="range" name="points" min="1" max="10"> <input type="submit"> </form> <p><b>注意:</b> Internet Explorer 9 及更早 IE 版本不支持 type="range"。</p> </body> </html>
请使用下面的属性来规定对数字类型的限定:
- max - 规定允许的最大值
- min - 规定允许的最小值
- step - 规定合法的数字间隔
- value - 规定默认值
Input 类型: search
search 类型用于搜索域,比如站点搜索或 Google 搜索。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> Search Google: <input type="search" name="googlesearch"><br> <input type="submit"> </form> </body> </html>
Input 类型: tel
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> 电话号码: <input type="tel" name="usrtel"><br> <input type="submit"> </form> </body> </html>
Input 类型: time
time 类型允许你选择一个时间。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> 选择时间: <input type="time" name="usr_time"> <input type="submit"> </form> </body> </html>
Input 类型: url
url 类型用于应该包含 URL 地址的输入域。在提交表单时,会自动验证 url 域的值。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> 添加你的主页: <input type="url" name="homepage"><br> <input type="submit"> </form> <p><b>注意:</b> Internet Explorer 9及更早 IE 版本不支持 type="url" 。</p> </body> </html>
提示: iPhone 中的 Safari 浏览器支持 url 输入类型,并通过改变触摸屏键盘来配合它(添加 .com 选项)。
Input 类型: week
week 类型允许你选择周和年。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>95知识库(995w.com)</title> </head> <body> <form action=""> 选择周: <input type="week" name="year_week"> <input type="submit"> </form> </body> </html>