HTML 5深入浅出教学篇之七

开发 前端
本文讲到的是HTML 5表单元素form, label, button, select, option, optgroup, datalist, textarea, fieldset, legend, progress, meter, keygen, output的使用

介绍

HTML 5: 表单元素

表单元素 - form, label, button, select, option, optgroup, datalist, textarea, fieldset, legend, progress, meter, keygen, output

表单验证

示例

1、form - 表单element/form/form.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>form</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         form - 表单,用于提交子元素数据到服务端  
  9.           accept-charset - 指定提交数据的编码格式  
  10.           action - 提交的目标地址  
  11.           autocomplete - 是否对所有子元素启用自动完成功能(on 或 off)  
  12.           enctype - 编码类型(application/x-www-form-urlencoded 或 multipart/form-data 或 text/plain)  
  13.           method - form 的 method(默认是 GET)  
  14.           name - form 的 name  
  15.           novalidate - 提交时是否不需要验证,boolean 类型  
  16.           target - form 的 target  
  17.     --> 
  18.     <form action="#"> 
  19.         <input type="text" name="txt" value="webabcd" /> 
  20.         <input type="submit" name="btn" value="submit" /> 
  21.     </form> 
  22. </body> 
  23. </html> 

2、label - 关联其它元素element/form/label.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>label</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         label - 关联其它元素  
  9.           form - 指定 label 所属的表单,多个用空格分开  
  10.           for - 关联元素的 id  
  11.  
  12.         DOM  
  13.           form, htmlFor  
  14.           control - 返回 label 所关联的元素  
  15.     --> 
  16.     <label><input type="checkbox" /> checkbox title</label> 
  17.     <br /> 
  18.     <input id="chk" type="checkbox" /> 
  19.     <label id="lbl" for="chk">checkbox title</label> 
  20.     <script type="text/javascript"> 
  21.         var lbl = document.getElementById("lbl");  
  22.         alert(document.getElementById("lbl").htmlFor);  
  23.         alert(document.getElementById("lbl").control.outerHTML);  
  24.     </script> 
  25. </body> 
  26. </html> 

3、button - 按钮元素element/form/button.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>button</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         button - 按钮元素  
  9.           autofocus - 页面加载后是否自动置为焦点,boolean 类型  
  10.           disabled - 是否无效,boolean 类型  
  11.           form - 指定关联的 form 的 id  
  12.           formaction - 指定关联 form 的 action  
  13.           formenctype - 指定关联 form 的编码类型  
  14.           formmethod - 指定关联 form 的 method  
  15.           formnovalidate - 指定关联 form 在提交时是否不需要验证,boolean 类型  
  16.           formtarget - 指定关联 form 的 target  
  17.           type - 按钮类型(button 或 submit 或 reset)  
  18.     -->   
  19.     <button type="button">button</button> 
  20. </body> 
  21. </html> 

4、select - 下拉列表框元素, option - 选项, optgroup - 选项组element/form/select_option_optgroup.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>select option optgroup</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         select - 下拉列表框元素  
  9.           autofocus, disabled, form, name, required, size  
  10.           multiple - 是否可多选,boolean 类型  
  11.         option - 选项  
  12.           disabled, label, selected, value  
  13.         optgroup - 选项组  
  14.           disabled, label  
  15.     --> 
  16.     <select> 
  17.         <option value="1" label="aaa" /> 
  18.         <option value="2" label="bbb" /> 
  19.         <option value="3" label="ccc" selected /> 
  20.         <option value="4" label="ddd" /> 
  21.         <option value="5" label="eee" /> 
  22.     </select> 
  23.     <select multiple> 
  24.         <option value="1">aaa</option> 
  25.         <option value="2">bbb </option> 
  26.         <option value="3" selected>ccc</option> 
  27.         <option value="4" selected>ddd</option> 
  28.         <option value="5">eee </option> 
  29.     </select> 
  30.     <select> 
  31.         <optgroup label="optgroup 1"> 
  32.             <option value="1">aaa</option> 
  33.             <option value="2">bbb </option> 
  34.         </optgroup> 
  35.         <optgroup label="optgroup 2"> 
  36.             <option value="3">ccc</option> 
  37.             <option value="4">ddd </option> 
  38.         </optgroup> 
  39.         <optgroup label="optgroup 3"> 
  40.             <option value="5" selected>eee</option> 
  41.             <option value="6">fff </option> 
  42.         </optgroup> 
  43.     </select> 
  44. </body> 
  45. </html> 

#p#

5、datalist - 数据列表元素, option - 数据项element/form/datalist_option.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>datalist option</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         datalist - 数据列表元素  
  9.         option - 数据项  
  10.           value - 数据项的值  
  11.           label - 数据项的说明  
  12.     --> 
  13.     <input type="email" list="contacts" /> 
  14.     <datalist id="contacts"> 
  15.         <option value="aabb@aa.com" label="张三" /> 
  16.         <option value="ccdd@cc.com" label="李四" /> 
  17.         <option value="eeff@ee.com" label="王五" /> 
  18.     </datalist> 
  19. </body> 
  20. </html> 

6、textarea - 文本区域元素element/form/textarea.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>textarea</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         textarea - 文本区域元素  
  9.           autofocus, dirname, disabled, maxlength, name, placeholder, readonly, required - 参考 /element/form/input/_attributes.html  
  10.           cols - 显示的列数(单位:字符数)  
  11.           rows - 显示的行数(单位:字符数)  
  12.           wrap - 换行方式  
  13.             soft - 需要换行则换行(相当于 wrap)  
  14.             hard - 强制不换行(相当于 nowrap)  
  15.     --> 
  16.     <textarea cols="3" rows="3"> 
  17. aaabbbccc  
  18.     </textarea> 
  19. </body> 
  20. </html> 

7、fieldset - 用于定义一个区域, legend - 用于定义一个区域的标题,它是 fieldset 的第一个子元素 element/form/fieldset_legend.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>fieldset legend</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         fieldset - 用于定义一个区域  
  9.           form - 指定所属表单,多个用空格分开  
  10.           disabled - 是否无效(Opera 支持此标准)  
  11.  
  12.         legend - 用于定义一个区域的标题,它是 fieldset 的第一个子元素   
  13.     --> 
  14.     <fieldset disabled> 
  15.         <legend> 
  16.             <label> 
  17.                 <input type="checkbox" /> title  
  18.             </label> 
  19.         </legend> 
  20.         <p> 
  21.             p1  
  22.         </p> 
  23.         <p> 
  24.             p2  
  25.         </p> 
  26.         <p> 
  27.             p3  
  28.         </p> 
  29.     </fieldset> 
  30. </body> 
  31. </html> 

8、progress - 进度元素 element/form/progress.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>progress</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         progress - 进度元素  
  9.           value - 当前进度值  
  10.           max - 进度的最大值  
  11.           form - 对应的 form 的 id  
  12.     --> 
  13.     <progress id="progress" max="100"></progress> 
  14.     <script type="text/javascript"> 
  15.         var progressBar = document.getElementById('progress');  
  16.         progressBar.value = 10;  
  17.     </script> 
  18. </body> 
  19. </html> 

#p#

9、meter - 用来表示一个范围已知且可度量的值 element/form/meter.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>meter</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         meter - 用来表示一个范围已知且可度量的值  
  9.           form - 对应的 form 的 id  
  10.           value - 当前值  
  11.           min - 最小值  
  12.           max - 最大值  
  13.           low - 低水平的值  
  14.           high - 高水平的值  
  15.           optimum - 最适宜的值  
  16.     -->      
  17.     <span>血糖含量:</span> 
  18.     <meter value="60" min="0" max="100" low="20" high="80" optimum="50" /> 
  19.  
  20. </body> 
  21. </html> 

10、keygen - 呈现出一个键值对生成器,提交表单后,公钥发往服务端,私钥保存在客户端element/form/keygen.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>keygen</title> 
  5. </head> 
  6. <body>      
  7.     <!--  
  8.         keygen - 呈现出一个键值对生成器,提交表单后,公钥发往服务端,私钥保存在客户端  
  9.           autofocus, challenge, disabled, form, keytype  
  10.     --> 
  11.     <keygen /> 
  12. </body> 
  13. </html> 

11、output - 用于呈现计算结果element/form/output.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>output</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         output - 用于呈现计算结果(必须要有起始和结束标记)  
  9.           form, name  
  10.           for - 关联元素的 id,可以有多个  
  11.     -->      
  12.     <output id="output"></output> 
  13.     <script type="text/javascript"> 
  14.         document.getElementById("output").value = 10 * 10;  
  15.     </script> 
  16. </body> 
  17. </html> 

12、表单验证element/form/_validate.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>表单验证</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         表单验证(Opera  支持此标准)  
  9.  
  10.         required - 指定是否为必填元素  
  11.         pattern - 用指定的正则表达式对 input 的值做验证  
  12.         url, email, number 等有验证功能的元素  
  13.  
  14.         element.validity - 返回验证状态,ValidityState 对象  
  15.         ValidityState - 验证状态对象  
  16.           valid - 是否通过了验证(以下 8 个值都为 false,则此值为 true),boolean 类型  
  17.           valueMissing - 是否没有值(对应的元素如果设置为必填但没有值的时候,此值为 true),boolean 类型  
  18.           typeMismatch - 是否值的类型与期望类型不匹配,boolean 类型  
  19.           patternMismatch - 是否正则表达式验证失败,boolean 类型  
  20.           tooLong - 是否字符过多,boolean 类型  
  21.           rangeUnderflow - 是否比预设的最小值还要小,boolean 类型  
  22.           rangeOverflow - 是否比预设的最大值还要大,boolean 类型  
  23.           stepMismatch - 是否不匹配 step 的设置(比如 step 为 3,那么如果值要匹配 step 的话,则一定要为 3 的倍数),boolean 类型  
  24.           customError - 是否有自定义错误信息,boolean 类型  
  25.  
  26.         element.setCustomValidity("错误信息") - 用于指定自定义的错误信息  
  27.         element.setCustomValidity("") - 用于清除自定义的错误信息  
  28.     --> 
  29.     <form action="#"> 
  30.         <input type="text" name="txt" id="txt" required /> 
  31.         <input type="email" name="email" id="email" /> 
  32.         <input type="submit" name="btn" value="submit" /> 
  33.         <br /> 
  34.         <input type="button" value="验证 email 元素" onclick="validateEmail();" /> 
  35.     </form> 
  36.     <script type="text/javascript"> 
  37.         function validateEmail() {  
  38.             var email = document.getElementById("email");  
  39.             var validityState = email.validity;  
  40.             alert  
  41.             (  
  42.                 validityState.valid + "\n" +  
  43.                 validityState.valueMissing + "\n" +  
  44.                 validityState.typeMismatch + "\n" +  
  45.                 validityState.patternMismatch + "\n" +  
  46.                 validityState.tooLong + "\n" +  
  47.                 validityState.rangeUnderflow + "\n" +  
  48.                 validityState.rangeOverflow + "\n" +  
  49.                 validityState.stepMismatch + "\n" +  
  50.                 validityState.customError  
  51.             );  
  52.         }  
  53.     </script> 
  54. </body> 
  55. </html> 

[源码下载]

原文链接:http://www.cnblogs.com/webabcd/archive/2012/02/08/2342275.html

责任编辑:张伟 来源: webabcd的博客
相关推荐

2012-05-30 13:49:52

HTML5

2012-05-30 14:51:09

HTML5

2012-05-31 09:54:13

HTML5

2012-05-31 10:57:06

HTML5

2012-05-30 13:26:12

HTML5

2012-05-31 09:19:22

HTML5

2012-05-31 09:35:43

HTML5

2012-05-30 11:11:42

HTML5

2012-05-30 13:17:46

HTML5

2012-05-30 10:52:09

HTML5

2009-11-18 13:30:37

Oracle Sequ

2009-11-17 17:31:58

Oracle COMM

2022-02-25 08:54:50

setState异步React

2021-03-16 08:54:35

AQSAbstractQueJava

2011-07-04 10:39:57

Web

2013-11-14 15:53:53

AndroidAudioAudioFlinge

2017-06-06 15:34:41

物联网数据库压缩

2017-06-05 14:50:33

大数据数据库压缩

2019-01-07 15:29:07

HadoopYarn架构调度器

2021-07-20 15:20:02

FlatBuffers阿里云Java
点赞
收藏

51CTO技术栈公众号