下面的表格列出了可插入 HTML 5 元素中以定義事件行為的標準事件屬性。
- Window 事件屬性 - Window Event Attributes
- 表單事件 - Form Events
- 鍵盤事件 - Keybord Events
- 鼠標事件 - Mouse Events
- 媒介事件 - Media Events
Window 事件屬性
window 對象觸發的事件。適用于 <body> 標簽:
| 屬性 | 值 | 描述 |
|---|---|---|
| onafterprint | script | 在打印文檔之后運行腳本 |
| onbeforeprint | script | 在文檔打印之前運行腳本 |
| onbeforeonload | script | 在文檔加載之前運行腳本 |
| onblur | script | 當窗口失去焦點時運行腳本 |
| onerror | script | 當錯誤發生時運行腳本 |
| onfocus | script | 當窗口獲得焦點時運行腳本 |
| onhaschange | script | 當文檔改變時運行腳本 |
| onload | script | 當文檔加載時運行腳本 |
| onmessage | script | 當觸發消息時運行腳本 |
| onoffline | script | 當文檔離線時運行腳本 |
| ononline | script | 當文檔上線時運行腳本 |
| onpagehide | script | 當窗口隱藏時運行腳本 |
| onpageshow | script | 當窗口可見時運行腳本 |
| onpopstate | script | 當窗口歷史記錄改變時運行腳本 |
| onredo | script | 當文檔執行再執行操作(redo)時運行腳本 |
| onresize | script | 當調整窗口大小時運行腳本 |
| onstorage | script | 當文檔加載加載時運行腳本 |
| onundo | script | 當 Web Storage 區域更新時(存儲空間中的數據發生變化時) |
| onunload | script | 當用戶離開文檔時運行腳本 |
表單事件
由 HTML 表單內部的動作觸發的事件。適用于所有 HTML 5 元素,不過最常用于表單元素中:
| 屬性 | 值 | 描述 |
|---|---|---|
| onblur | script | 當元素失去焦點時運行腳本 |
| onchange | script | 當元素改變時運行腳本 |
| oncontextmenu | script | 當觸發上下文菜單時運行腳本 |
| onfocus | script | 當元素獲得焦點時運行腳本 |
| onformchange | script | 當表單改變時運行腳本 |
| onforminput | script | 當表單獲得用戶輸入時運行腳本 |
| oninput | script | 當元素獲得用戶輸入時運行腳本 |
| oninvalid | script | 當元素無效時運行腳本 |
| onreset | script | 當表單重置時運行腳本。HTML 5 不支持。 |
| onselect | script | 當選取元素時運行腳本 |
| onsubmit | script | 當提交表單時運行腳本 |
鍵盤事件
由鍵盤觸發的事件。適用于所有 HTML 5 元素:
| 屬性 | 值 | 描述 |
|---|---|---|
| onkeydown | script | 當按下按鍵時運行腳本 |
| onkeypress | script | 當按下并松開按鍵時運行腳本 |
| onkeyup | script | 當松開按鍵時運行腳本 |
鼠標事件
由鼠標或相似的用戶動作觸發的事件。適用于所有 HTML 5 元素:
| 屬性 | 值 | 描述 |
|---|---|---|
| onclick | script | 當單擊鼠標時運行腳本 |
| ondblclick | script | 當雙擊鼠標時運行腳本 |
| ondrag | script | 當拖動元素時運行腳本 |
| ondragend | script | 當拖動操作結束時運行腳本 |
| ondragenter | script | 當元素被拖動至有效的拖放目標時運行腳本 |
| ondragleave | script | 當元素離開有效拖放目標時運行腳本 |
| ondragover | script | 當元素被拖動至有效拖放目標上方時運行腳本 |
| ondragstart | script | 當拖動操作開始時運行腳本 |
| ondrop | script | 當被拖動元素正在被拖放時運行腳本 |
| onmousedown | script | 當按下鼠標按鈕時運行腳本 |
| onmousemove | script | 當鼠標指針移動時運行腳本 |
| onmouseout | script | 當鼠標指針移出元素時運行腳本 |
| onmouseover | script | 當鼠標指針移至元素之上時運行腳本 |
| onmouseup | script | 當松開鼠標按鈕時運行腳本 |
| onmousewheel | script | 當轉動鼠標滾輪時運行腳本 |
| onscroll | script | 當滾動元素滾動元素的滾動條時運行腳本 |
媒介事件
由視頻、圖像以及音頻等媒介觸發的事件。適用于所有 HTML 5 元素,不過在媒介元素(諸如 audio、embed、img、object 以及 video)中最常用:
| 屬性 | 值 | 描述 |
|---|---|---|
| onabort | script | 當發生中指事件時運行腳本 |
| oncanplay | script | 當媒介能夠開始播放但可能因緩沖而需要停止時運行腳本 |
| oncanplaythrough | script | 當媒介能夠無需因緩沖而停止即可播放至結尾時運行腳本 |
| ondurationchange | script | 當媒介長度改變時運行腳本 |
| onemptied | script | 當媒介資源元素突然為空時(網絡錯誤、加載錯誤等)運行腳本 |
| onended | script | 當媒介已抵達結尾時運行腳本 |
| onerror | script | 當在元素加載期間發生錯誤時運行腳本 |
| onloadeddata | script | 當加載媒介數據時運行腳本 |
| onloadedmetadata | script | 當媒介元素的持續時間以及其他媒介數據已加載時運行腳本 |
| onloadstart | script | 當瀏覽器開始加載媒介數據時運行腳本 |
| onpause | script | 當媒介數據暫停時運行腳本 |
| onplay | script | 當媒介數據將要開始播放時運行腳本 |
| onplaying | script | 當媒介數據已開始播放時運行腳本 |
| onprogress | script | 當瀏覽器正在取媒介數據時運行腳本 |
| onratechange | script | 當媒介數據的播放速率改變時運行腳本 |
| onreadystatechange | script | 當就緒狀態(ready-state)改變時運行腳本 |
| onseeked | script | 當媒介元素的定位屬性[1]不再為真且定位已結束時運行腳本 |
| onseeking | script | 當媒介元素的定位屬性為真且定位已開始時運行腳本 |
| onstalled | script | 當取回媒介數據過程中(延遲)存在錯誤時運行腳本 |
| onsuspend | script | 當瀏覽器已在取媒介數據但在取回整個媒介文件之前停止時運行腳本 |
| ontimeupdate | script | 當媒介改變其播放位置時運行腳本 |
| onvolumechange | script | 當媒介改變音量亦或當音量被設置為靜音時運行腳本 |
| onwaiting | script | 當媒介已停止播放但打算繼續播放時運行腳本 |
[1]:定位屬性的英文譯文是:seeking attribute。
以上這篇HTML5事件方法全部匯總就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。



















