自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HTML 5深入淺出教學篇之七

開發(fā) 前端
本文講到的是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 - 表單,用于提交子元素數(shù)據(jù)到服務(wù)端  
  9.           accept-charset - 指定提交數(shù)據(jù)的編碼格式  
  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 - 關(guān)聯(lián)其它元素element/form/label.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>label</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         label - 關(guān)聯(lián)其它元素  
  9.           form - 指定 label 所屬的表單,多個用空格分開  
  10.           for - 關(guān)聯(lián)元素的 id  
  11.  
  12.         DOM  
  13.           form, htmlFor  
  14.           control - 返回 label 所關(guān)聯(lián)的元素  
  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 - 指定關(guān)聯(lián)的 form 的 id  
  12.           formaction - 指定關(guān)聯(lián) form 的 action  
  13.           formenctype - 指定關(guān)聯(lián) form 的編碼類型  
  14.           formmethod - 指定關(guān)聯(lián) form 的 method  
  15.           formnovalidate - 指定關(guān)聯(lián) form 在提交時是否不需要驗證,boolean 類型  
  16.           formtarget - 指定關(guān)聯(lián) 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 - 數(shù)據(jù)列表元素, option - 數(shù)據(jù)項element/form/datalist_option.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>datalist option</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         datalist - 數(shù)據(jù)列表元素  
  9.         option - 數(shù)據(jù)項  
  10.           value - 數(shù)據(jù)項的值  
  11.           label - 數(shù)據(jù)項的說明  
  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 - 文本區(qū)域元素element/form/textarea.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>textarea</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         textarea - 文本區(qū)域元素  
  9.           autofocus, dirname, disabled, maxlength, name, placeholder, readonly, required - 參考 /element/form/input/_attributes.html  
  10.           cols - 顯示的列數(shù)(單位:字符數(shù))  
  11.           rows - 顯示的行數(shù)(單位:字符數(shù))  
  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 - 用于定義一個區(qū)域, legend - 用于定義一個區(qū)域的標題,它是 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 - 用于定義一個區(qū)域  
  9.           form - 指定所屬表單,多個用空格分開  
  10.           disabled - 是否無效(Opera 支持此標準)  
  11.  
  12.         legend - 用于定義一個區(qū)域的標題,它是 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 - 呈現(xiàn)出一個鍵值對生成器,提交表單后,公鑰發(fā)往服務(wù)端,私鑰保存在客戶端element/form/keygen.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>keygen</title> 
  5. </head> 
  6. <body>      
  7.     <!--  
  8.         keygen - 呈現(xiàn)出一個鍵值對生成器,提交表單后,公鑰發(fā)往服務(wù)端,私鑰保存在客戶端  
  9.           autofocus, challenge, disabled, form, keytype  
  10.     --> 
  11.     <keygen /> 
  12. </body> 
  13. </html> 

11、output - 用于呈現(xiàn)計算結(jié)果element/form/output.html

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <title>output</title> 
  5. </head> 
  6. <body> 
  7.     <!--  
  8.         output - 用于呈現(xiàn)計算結(jié)果(必須要有起始和結(jié)束標記)  
  9.           form, name  
  10.           for - 關(guān)聯(lián)元素的 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 - 返回驗證狀態(tài),ValidityState 對象  
  15.         ValidityState - 驗證狀態(tài)對象  
  16.           valid - 是否通過了驗證(以下 8 個值都為 false,則此值為 true),boolean 類型  
  17.           valueMissing - 是否沒有值(對應的元素如果設(shè)置為必填但沒有值的時候,此值為 true),boolean 類型  
  18.           typeMismatch - 是否值的類型與期望類型不匹配,boolean 類型  
  19.           patternMismatch - 是否正則表達式驗證失敗,boolean 類型  
  20.           tooLong - 是否字符過多,boolean 類型  
  21.           rangeUnderflow - 是否比預設(shè)的最小值還要小,boolean 類型  
  22.           rangeOverflow - 是否比預設(shè)的最大值還要大,boolean 類型  
  23.           stepMismatch - 是否不匹配 step 的設(shè)置(比如 step 為 3,那么如果值要匹配 step 的話,則一定要為 3 的倍數(shù)),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的博客
相關(guān)推薦

2012-05-30 14:51:09

HTML5

2012-05-31 09:19:22

HTML5

2012-05-31 10:57:06

HTML5

2012-05-30 13:26:12

HTML5

2012-05-30 13:49:52

HTML5

2012-05-31 09:54:13

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-05 14:50:33

大數(shù)據(jù)數(shù)據(jù)庫壓縮

2017-06-06 15:34:41

物聯(lián)網(wǎng)數(shù)據(jù)庫壓縮

2012-05-21 10:06:26

FrameworkCocoa

2021-07-20 15:20:02

FlatBuffers阿里云Java
點贊
收藏

51CTO技術(shù)棧公眾號