第 10 章 表单元素[中]
学习要点: 1.type 属性总汇 2.type 属性解析
主讲教师:李炎恢
本章主要探讨 HTML5 中表单中 input 元素的 type 属性,根据不同的值来显示不同的输入框。
一.type 属性总汇 input 元素可以用来生成一个供用户输入数据的简单文本框。在默认的情况下,什么样的数据均可以输入。而通过不同的属性值,可以限制输入的内容。
二.input 元素解析 1.type 为 text 值时 <input type="text"> 解释:当 type 值为 text 时,呈现的就是一个可以输入任意字符的文本框,这也是默认行为。并且,还提供了一些额外的属性。
//设置文本框长度 <input type="text" size="50">
//设置文本框输入字符长度 <input type="text" maxlength="10">
//设置文本框的初始值 <input type="text" value="初始值">
//设置文本框输入提示 <input type="text" placeholder="请输入内容">
//设置文本提供的建议值 <input list="footlist"><datalist id="footlist"> <option value="苹果">苹果option> <option value="桔子">桔子option> <option value="香蕉" label="香蕉"> <option value="梨子">datalist>
//设置文本框内容为只读,可以提交数据 <input type="text" readonly>
//设置文本框内容不可用,不可以提交数据 <input type="text" disabled>
2.type 为 password 值时 <input type="password"> 解释:当 type 值为 password 时,一般用于密码框的输入,所有的字符都会显示星号。密码框也有一些额外属性。
这里除了正则和验证需要放在下一节,其余和文本框一致。
3.type 为 search 时 <input type="search"> 解释:和文本框一致,在除 Firefox 浏览器的其他现代浏览器,会显示一个叉来取消搜索内容。额外属性也和 text 一致。
4.type 为 number、range 时 <input type="number"><input type="range"> 解释:只限输入数字的文本框,不同浏览器可能显示方式不同。生成一个数值范围文本框,只是样式是拖动式。额外属性如下:
//范围和步长 <input type="number" step="2" min="10" max="100">
5.type 为 date 系列时 <input type="date"><input type="month"><input type="time"><input type="week"><input type="datetime"><input type="datetime-local"> 解释:实现文本框可以获取日期和时间的值,但支持的浏览器不完整。我们测试 Chrome 和 Opera 支持,其他浏览器尚未支持。所以,在获取日期和时间,目前还是推荐使用 jQuery 等前端库来实现日历功能。额外属性和 number 一致。
6.type 为 color 时 <input type="color"> 解释:实现文本框获取颜色的功能,最新的现代浏览器测试后 IE 不支持,其余的都能显示一个颜色对话框提供选择。
7.type 为 checkbox、radio 时 音乐<input type="checkbox">体育<input type="checkbox"><input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女
解释:生成一个获取布尔值的复选框或固定选项的单选框。额外属性如下:
//默认勾选,默认值为 1 <input type="checkbox" name="music" checked value="1">
8.type 为 submit、reset 和 button 时 <input type="submit"> 解释:生成一个按钮,三种模式:提交、重置和一般按钮,和
如果是 submit 时,还提供了和
9.type 为 image 时 <input type="image" src="img.png"> 解释:生成一个图片按钮,点击图片就实现提交功能,并且传送了分区响应数据。图片按钮也提供了一些额外属性。
10.type 为 email、tel、url 时 <input type="email"><input type="tel"><input type="url"> 解释:email 为电子邮件格式、tel 为电话格式、url 为网址格式。额外属性和 text 一致。但对于这几种类型,浏览器支持是不同的。email 支持比较好,现在浏览器都支持格式验证;tel 基本不支持;url 支持一般,部分浏览器只要检测到 http://就能通过。
11.type 为 hidden 时 <input type="hidden"> 解释:生成一个隐藏控件,一般用于表单提交时关联主键 ID 提交,而这个数据作为隐藏存在。
12.type 为 file 时 <input type="file"> 解释:生成一个文件上传控件,用于文件的上传。额外提供了一些属性:
accept="image/gif, image/jpeg, image/png" |
- 上一篇:css文字环绕图片的方法
- 下一篇:响应式框架中,table表头自动换行的解决办法