萬盛學電腦網

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

HTML 標籤屬性完整參考:全域屬性、事件屬性、ARIA 無障礙屬性、表單驗證屬性一覽表

HTML 標籤屬性系統化速查:掌握屬性分類與核心用法

HTML 屬性分為全域屬性 (所有元素通用)、元素專屬屬性、事件處理屬性、ARIA 無障礙屬性、表單驗證屬性。理解分類有助於精準套用與除錯。

全域屬性 (Global Attributes) - 所有元素通用

屬性用途範例
classCSS/JS 掛鉤class="btn btn-primary"
id唯一識別碼id="main-nav"
style內嵌樣式 (不建議)color: red;
title提示文字title="點擊查看詳情"
lang元素語言lang="en"
dir文字方向dir="rtl"
hidden隱藏元素hidden 或 hidden="until-found"
data-*自訂資料屬性data-user-id="123"
tabindex鍵盤焦點順序tabindex="0" (納入順序), -1 (移除)
contenteditable可編輯內容contenteditable="plaintext-only"
spellcheck拼字檢查spellcheck="true"
draggable拖放 APIdraggable="true"
slotWeb Components 插槽slot="header"
is自訂元素擴充<button is="my-button">
partShadow DOM 樣式暴露part="icon"
exportpartsShadow DOM 部分轉發exportparts="icon:btn-icon"

事件處理屬性 (Event Handlers) - 建議改用 addEventListener

  • 滑鼠:onclick, ondblclick, onmousedown/up/move/over/out/enter/leave, onwheel, oncontextmenu
  • 鍵盤:onkeydown/up/press
  • 表單:onsubmit, onreset, onchange, oninput, onfocus/blur, oninvalid
  • 剪貼簿:oncopy/cut/paste
  • 媒體:onplay/pause/ended/timeupdate/volumechange
  • 視窗:onload/error/resize/scroll (僅 body/window)
  • 拖放:ondrag/start/end/enter/leave/over/drop
  • 指標鎖定:onpointerdown/up/move/over/out/enter/leave/cancel/got/lost

最佳實踐:HTML 避免內聯事件,改用 element.addEventListener('click', handler) 關注點分離、支援多監聽器、便於移除。

ARIA 無障礙屬性 (僅在原生語意不足時補充)

分類屬性範例
角色role="button|dialog|menu|tablist|alert|status|region|search|navigation"
狀態/屬性aria-expanded, aria-hidden, aria-disabled, aria-selected, aria-checked, aria-pressed, aria-current, aria-live="polite|assertive", aria-atomic, aria-busy
關聯aria-labelledby, aria-describedby, aria-controls, aria-owns, aria-activedescendant
輸入aria-required, aria-invalid, aria-placeholder, aria-autocomplete

原則:優先使用原生語意標籤 (<button>, <nav>, <main>),原生已內建 ARIA 語意。

表單驗證與互動屬性

屬性適用說明
requiredinput, select, textarea必填,瀏覽器原生驗證
patterninput (text, tel, email, url, password)RegExp 模式驗證,需配合 title 提示
min/max/stepinput (number, date, range, time)數值/日期範圍與步進
minlength/maxlengthinput, textarea字元長度限制
multipleinput (file, email), select允許多值
acceptinput[type=file]限制檔案類型 (MIME/副檔名)
autocompleteinput, form瀏覽器自動填入提示 (on/off/email/username/new-password...)
novalidateform停用瀏覽器原生驗證 (自行實作)
forminput, select, textarea, button, output, label, fieldset關聯非巢狀 form ID
formaction/formenctype/formmethod/formtarget/formnovalidatebutton, input[type=submit/image]覆寫 form 屬性

核心元素專屬屬性精選

  • <a>:href, target, rel, download, hreflang, type, referrerpolicy
  • <img>:src, alt, srcset, sizes, loading, decoding, fetchpriority, width, height, crossorigin, referrerpolicy
  • <input>:type, name, value, placeholder, disabled, readonly, autofocus, list (關聯 datalist)
  • <meta>:charset, name, content, http-equiv, property (Open Graph)
  • <link>:rel, href, as, crossorigin, media, sizes, type, fetchpriority
  • <script>:src, type (module/importmap), async, defer, nomodule, crossorigin, integrity (SRI), referrerpolicy, fetchpriority
copyright © 萬盛學電腦網 all rights reserved