萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML <input> 標籤類型與屬性速查表(補充版):新增類型、驗證屬性、無障礙要點

HTML5 新增 input type(續)

type適用場景移動端鍵盤驗證/值格式
datetime-local本地日期時間(無時區)日期時間選擇器YYYY-MM-DDThh:mm
month年月月份選擇器YYYY-MM
week年周周選擇器YYYY-Www
time時間時間選擇器hh:mm 或 hh:mm:ss
color顏色選擇原生拾色器#RRGGBB 小寫
search搜索框帶清除鈕、圓角文本,可配 incremental
tel電話號碼撥號盤無格式強制,配合 pattern

進階驗證屬性

屬性類型說明示例
patternRegex自定義格式正則,需配合 title 提示pattern="[0-9]{6}" title="6位數字驗證碼"
min/maxNumber/Date最小/最大值(含邊界)min="18" max="99"
stepNumber步長/精度step="0.01" (價格)
minlength/maxlengthInteger字符長度限制minlength="8" maxlength="20"
requiredBoolean必填,提交時瀏覽器原生攔截required
multipleBooleanemail/file 允許多值<input type="file" multiple>
acceptMIME/Ext文件類型過濾accept="image/*,.pdf"
autocompleteToken瀏覽器自動填充提示autocomplete="new-password"

無障礙關鍵:Label 綁定與 ARIA

<!-- 顯式綁定(推薦) -->
<label for="user-name">姓名</label>
<input type="text" id="user-name" name="name" required>

<!-- 隱式包裹 -->
<label>郵箱 <input type="email" name="email"></label>

<!-- 無法用 label 時:aria-label / aria-labelledby -->
<input type="text" aria-label="搜索關鍵詞" placeholder="搜索...">
<span id="pwd-desc">至少8位,含大小寫數字</span>
<input type="password" aria-describedby="pwd-desc">

表單提交數據格式

  • application/x-www-form-urlencoded(默認):鍵值對編碼。
  • multipart/form-data:文件上傳必須。
  • text/plain:罕用。
  • JS 獲取:new FormData(form) → fetch(url, {method:'POST', body: formData})。

樣式鉤子偽類

input:valid { border-color: green; }
input:invalid { border-color: red; }
input:required { background: #fffde7; }
input:focus:invalid { outline: 2px solid red; }
input:user-invalid { /* Firefox: 用戶交互後才顯示無效 */ }
copyright © 萬盛學電腦網 all rights reserved