在 HTML5 對既有元素的強化當中,改動幅度最大的莫過於表單。過去必須靠 JavaScript 才能做到的一批功能——日期挑選器、範圍滑桿、自動驗證、提示文字——現在幾乎都能純宣告式完成。以下依元素逐一梳理新增的屬性與型別。
在 HTML5 中,表單控制項可以位於它所屬表單的外部。因此 fieldset、label、input 等元素都新增了 form 屬性,用來標示自己歸屬於哪一個表單(值為表單的 id,且可以同時屬於多個)。這個特性讓版面安排自由度大幅提升,不必再為了結構而硬把控制項塞進 <form> 裡。
autocomplete:啟用「下拉建議清單」,讓瀏覽器自動帶出使用者先前輸入過的內容。novalidate:關閉整個表單的驗證。開發測試階段非常好用,省去每次都要填完必填欄位的麻煩。disabled 可一次停用群組內的所有控制項,name 用於設定群組名稱。至於 form,當 fieldset 被放到表單外面時就必須設定,這樣它才能正確與一個或多個表單關聯起來。
除了新增 form 之外,label 只有既有的 for 屬性值得留意。它用來指定該標籤所附屬的控制項 id,按下標籤文字時,對應的控制項就會獲得焦點。
<form action="demo_form.asp" id="form1">
<label for="name">Click Me</label>
<input id="name" type="text" />
<input type="submit" value="Submit" />
</form>
點擊「Click Me」,後方的輸入框就會取得焦點。這個屬性對無障礙設計與行動裝置的使用體驗幫助很大,卻常被忽略。
除了原有的 button、text、submit、checkbox、radio、select、password,HTML5 一口氣加入了下列型別:
colordate、datetime、datetime-local、month、week、timeemailnumberrangesearchtelurl這些型別的價值在於語意化:瀏覽器與作業系統能據此提供合適的鍵盤面板、挑選器與內建驗證。可以跑下面的範例,實際比較各瀏覽器的支援情形。
<form action="demo_form.asp">
Select your favorite color: <input type="color" name="favcolor" />
Birthday: <input type="date" name="bday" />
Birthday (date and time): <input type="datetime" name="bdaytime" />
Birthday (date and time): <input type="datetime-local" name="bdaytime" />
Birthday (month and year): <input type="month" name="bdaymonth" />
Select a time: <input type="time" name="usr_time" />
Select a week: <input type="week" name="week_year" />
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5" />
Quantity (between 1 and 10):
<input type="range" name="points" min="1" max="10" />
Search Google: <input type="search" name="googlesearch" />
Telephone: <input type="tel" name="usrtel" />
Add your homepage: <input type="url" name="homepage" />
E-mail: <input type="email" name="usremail" />
<input type="submit" value="送出" />
</form>
_blank。datalist 元素提供候選清單,可用於 text、search、url、tel、email、date、number、range 與 color。<fieldset>
<legend>Favorites</legend>
<p>
<label>
<input type="text" name="favorites" list="options" />
<datalist id="options">
<option value="A">
<option value="B">
<option value="C">
</datalist>
</label>
</p>
</fieldset>
<form action="demo_form.asp">
E-mail: <input type="email" name="email" autocomplete="on" />
Image: <input type="image" src="img_submit.gif" alt="Submit"
width="48" height="48" />
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31" />
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02" />
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5" />
Select images: <input type="file" name="img" multiple="multiple" />
Country code:
<input type="text" name="country_code" pattern="[A-Za-z]{3}"
title="Three letter country code" />
First Name: <input type="text" name="fname" placeholder="First name" />
Username: <input type="text" name="usrname" required />
Number: <input type="number" name="points" step="3" />
<input type="submit" />
<input type="submit" formaction="demo_admin.asp" value="以管理者身分送出" />
<input type="submit" formenctype="multipart/form-data"
value="以 multipart 編碼送出" />
<input type="submit" formmethod="post" formaction="demo_post.asp"
value="以 POST 送出" />
<input type="submit" formnovalidate value="不驗證直接送出" />
<input type="submit" formtarget="_blank" value="在新視窗開啟" />
</form>
<form action="demo_form.asp" id="form1">
First name: <input type="text" name="fname" />
<input type="submit" value="Submit" />
</form>
Last name: <input type="text" name="lname" form="form1" />
最後一行就是前面提到的「控制項在表單外部」的實際寫法:lname 雖然寫在 <form> 之外,靠著 form="form1" 一樣會隨該表單一起送出。
雖然不是每個瀏覽器都支援全部型別,仍然鼓勵大家使用這些新語法。關鍵在於 HTML5 的降級設計:瀏覽器若認不得某個 type,只會退化成普通的 text 輸入框,不會報錯也不會壞版面。也就是說,採用新語法的成本幾乎為零,卻能在支援的環境中直接獲得更好的體驗。
延伸資源:
W3Schools HTML5 教學:https://www.w3schools.com/html5/default.asp
HTML5 官方指導文件:https://dev.w3.org/html5/html-author/
HTML5 Doctor:https://html5doctor.com/
HTML5 中文教學:https://www.html5china.com/