單行文字框是表單裡最常見的元件,登入、註冊、搜尋几乎都看得到它。
語法:
<input type="text"/>
舉例:
<!DOCTYPE html> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form name="form1" method="post" action="index.html"> 姓名:<input type="text"/> </form> </body> </html>
| 屬性 | 說明 |
|---|---|
| value | 定義文字框的預設值,也就是框內的初始文字 |
| size | 定義文字框的長度,以字元為單位 |
| maxlength | 限制最多可輸入的字元數 |
屬性順序不拘。input 另有 name 屬性,在 XHTML 中已被摒棄,因此只需掌握這三個。
<input type="text" value="預設文字" size="文字框長度" maxlength="最多輸入字元數"/>
舉例:
<!DOCTYPE html> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form name="form1" method="post" action="index.html"> 姓名:<input type="text" value="" size="15" maxlength=""/><br/> 年齡:<input type="text" value="18" size="3" maxlength="3"/> </form> </body> </html>
分析:「年齡」那格預設值為 18,maxlength 設為 3,代表最多只能打三個字元——畢竟沒有人的歲數是四位數。你可以在線上測試工具裡試著多打幾個數字,瀏覽器會硬性擋住。