# HTML 事件屬性
## 全局事件屬性
HTML 4 增加了使事件在瀏覽器中觸發動作的能力,比如當用戶點擊元素時啟動 JavaScript。
如需學習更多有關事件編程的知識,請訪問我們的 JavaScript 教程。
下面列出了添加到 HTML 元素以定義事件動作的全局事件屬性。
= HTML5 中新的事件屬性。
## Window 事件屬性
針對 window 對象觸發的事件(應用到 <body> 標簽):
| 屬性 | 值 | 描述 |
| --- | --- | --- |
| [onafterprint](/tags/event_onafterprint.asp "HTML onafterprint 事件屬性") | script | 文檔打印之后運行的腳本。 |
| [onbeforeprint](/tags/event_onbeforeprint.asp "HTML onbeforeprint 事件屬性") | script | 文檔打印之前運行的腳本。 |
| onbeforeunload | script | 文檔卸載之前運行的腳本。 |
| onerror | script | 在錯誤發生時運行的腳本。 |
| onhaschange | script | 當文檔已改變時運行的腳本。 |
| [onload](/tags/event_onload.asp "HTML onload 事件屬性") | script | 頁面結束加載之后觸發。 |
| onmessage | script | 在消息被觸發時運行的腳本。 |
| onoffline | script | 當文檔離線時運行的腳本。 |
| ononline | script | 當文檔上線時運行的腳本。 |
| onpagehide | script | 當窗口隱藏時運行的腳本。 |
| onpageshow | script | 當窗口成為可見時運行的腳本。 |
| onpopstate | script | 當窗口歷史記錄改變時運行的腳本。 |
| onredo | script | 當文檔執行撤銷(redo)時運行的腳本。 |
| [onresize](/tags/event_onresize.asp "HTML onresize 事件屬性") | script | 當瀏覽器窗口被調整大小時觸發。 |
| onstorage | script | 在 Web Storage 區域更新后運行的腳本。 |
| onundo | script | 在文檔執行 undo 時運行的腳本。 |
| [onunload](/tags/event_onunload.asp "HTML onunload 事件屬性") | script | 一旦頁面已下載時觸發(或者瀏覽器窗口已被關閉)。 |
## Form 事件
由 HTML 表單內的動作觸發的事件(應用到幾乎所有 HTML 元素,但最常用在 form 元素中):
| 屬性 | 值 | 描述 |
| --- | --- | --- |
| [onblur](/tags/event_onblur.asp "HTML onblur 事件屬性") | script | 元素失去焦點時運行的腳本。 |
| [onchange](/tags/event_onchange.asp "HTML onchange 事件屬性") | script | 在元素值被改變時運行的腳本。 |
| oncontextmenu | script | 當上下文菜單被觸發時運行的腳本。 |
| [onfocus](/tags/event_onfocus.asp "HTML onfocus 事件屬性") | script | 當元素失去焦點時運行的腳本。 |
| onformchange | script | 在表單改變時運行的腳本。 |
| onforminput | script | 當表單獲得用戶輸入時運行的腳本。 |
| oninput | script | 當元素獲得用戶輸入時運行的腳本。 |
| oninvalid | script | 當元素無效時運行的腳本。 |
| onreset | script | 當表單中的重置按鈕被點擊時觸發。HTML5 中不支持。 |
| [onselect](/tags/event_onselect.asp "HTML onselect 事件屬性") | script | 在元素中文本被選中后觸發。 |
| [onsubmit](/tags/event_onsubmit.asp "HTML onsubmit 事件屬性") | script | 在提交表單時觸發。 |
## Keyboard 事件
| 屬性 | 值 | 描述 |
| --- | --- | --- |
| [onkeydown](/tags/event_onkeydown.asp "HTML onkeydown 事件屬性") | script | 在用戶按下按鍵時觸發。 |
| [onkeypress](/tags/event_onkeypress.asp "HTML onkeypress 事件屬性") | script | 在用戶敲擊按鈕時觸發。 |
| [onkeyup](/tags/event_onkeyup.asp "HTML onkeyup 事件屬性") | script | 當用戶釋放按鍵時觸發。 |
## Mouse 事件
由鼠標或類似用戶動作觸發的事件:
| 屬性 | 值 | 描述 |
| --- | --- | --- |
| [onclick](/tags/event_onclick.asp "HTML onclick 事件屬性") | script | 元素上發生鼠標點擊時觸發。 |
| [ondblclick](/tags/event_ondblclick.asp "HTML ondblclick 事件屬性") | script | 元素上發生鼠標雙擊時觸發。 |
| ondrag | script | 元素被拖動時運行的腳本。 |
| ondragend | script | 在拖動操作末端運行的腳本。 |
| ondragenter | script | 當元素元素已被拖動到有效拖放區域時運行的腳本。 |
| ondragleave | script | 當元素離開有效拖放目標時運行的腳本。 |
| ondragover | script | 當元素在有效拖放目標上正在被拖動時運行的腳本。 |
| ondragstart | script | 在拖動操作開端運行的腳本。 |
| ondrop | script | 當被拖元素正在被拖放時運行的腳本。 |
| [onmousedown](/tags/event_onmousedown.asp "HTML onmousedown 事件屬性") | script | 當元素上按下鼠標按鈕時觸發。 |
| [onmousemove](/tags/event_onmousemove.asp "HTML onmousemove 事件屬性") | script | 當鼠標指針移動到元素上時觸發。 |
| [onmouseout](/tags/event_onmouseout.asp "HTML onmouseout 事件屬性") | script | 當鼠標指針移出元素時觸發。 |
| [onmouseover](/tags/event_onmouseover.asp "HTML onmouseover 事件屬性") | script | 當鼠標指針移動到元素上時觸發。 |
| [onmouseup](/tags/event_onmouseup.asp "HTML onmouseup 事件屬性") | script | 當在元素上釋放鼠標按鈕時觸發。 |
| onmousewheel | script | 當鼠標滾輪正在被滾動時運行的腳本。 |
| onscroll | script | 當元素滾動條被滾動時運行的腳本。 |
## Media 事件
由媒介(比如視頻、圖像和音頻)觸發的事件(適用于所有 HTML 元素,但常見于媒介元素中,比如 <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 | 每當就緒狀態改變時運行的腳本(就緒狀態監測媒介數據的狀態)。 |
| onseeked | script | 當 seeking 屬性設置為 false(指示定位已結束)時運行的腳本。 |
| onseeking | script | 當 seeking 屬性設置為 true(指示定位是活動的)時運行的腳本。 |
| onstalled | script | 在瀏覽器不論何種原因未能取回媒介數據時運行的腳本。 |
| onsuspend | script | 在媒介數據完全加載之前不論何種原因終止取回媒介數據時運行的腳本。 |
| ontimeupdate | script | 當播放位置改變時(比如當用戶快進到媒介中一個不同的位置時)運行的腳本。 |
| onvolumechange | script | 每當音量改變時(包括將音量設置為靜音)時運行的腳本。 |
| onwaiting | script | 當媒介已停止播放但打算繼續播放時(比如當媒介暫停已緩沖更多數據)運行腳本 |
- HTML 標簽列表
- HTML <!--...--> 標簽
- HTML <!DOCTYPE> 標簽
- HTML <a> 標簽
- HTML <abbr> 標簽
- HTML <acronym> 標簽
- HTML <address> 標簽
- HTML <applet> 標簽
- HTML <area> 標簽
- HTML <article> 標簽
- HTML <aside> 標簽
- HTML <audio> 標簽
- HTML <b> 標簽
- HTML <base> 標簽
- HTML <basefont> 標簽
- HTML <bdi> 標簽
- HTML <bdo> 標簽
- HTML <big> 標簽
- HTML <blockquote> 標簽
- HTML <body> 標簽
- HTML <br> 標簽
- HTML <button> 標簽
- HTML <canvas> 標簽
- HTML <caption> 標簽
- HTML <center> 標簽
- HTML <cite> 標簽
- HTML <em> <strong> <dfn> <code> <samp> <kbd><var> <cite> 標簽
- HTML <col> 標簽
- HTML <colgroup> 標簽
- HTML <command> 標簽
- HTML <datalist> 標簽
- HTML <dd> 標簽
- HTML <del> 標簽
- HTML <details> 標簽
- HTML <dialog> 標簽
- HTML <dir> 標簽
- HTML <div> 標簽
- HTML <dl> 標簽
- HTML <dt> 標簽
- HTML <embed> 標簽
- HTML <fieldset> 標簽
- HTML <figcaption> 標簽
- HTML <figure> 標簽
- HTML <font> 標簽
- HTML <footer> 標簽
- HTML <form> 標簽
- HTML <frame> 標簽
- HTML <frameset> 標簽
- HTML <h1> 到 <h6> 標簽
- HTML <head> 標簽
- HTML <header> 標簽
- HTML <hr> 標簽
- HTML <html> 標簽
- HTML <i> 標簽
- HTML <iframe> 標簽
- HTML <img> 標簽
- HTML <input> 標簽
- HTML DOM Button 對象
- HTML DOM Checkbox 對象
- HTML DOM Color 對象
- HTML DOM Input Date 對象
- HTML DOM Datetime 對象
- HTML DOM Datetime Local 對象
- HTML DOM Email 對象
- HTML DOM FileUpload 對象
- HTML DOM Hidden 對象
- HTML DOM Input Image 對象
- HTML DOM Month 對象
- HTML DOM Number 對象
- HTML DOM Password 對象
- HTML DOM Input Range 對象
- HTML DOM Radio 對象
- HTML DOM Reset 對象
- HTML DOM Input Search 對象
- HTML DOM Submit 對象
- HTML DOM Text 對象
- HTML DOM Input Time 對象
- HTML DOM Input URL 對象
- HTML DOM Input Week 對象
- HTML <ins> 標簽
- HTML <keygen> 標簽
- HTML <label> 標簽
- HTML <legend> 標簽
- HTML <li> 標簽
- HTML <link> 標簽
- HTML <main> 標簽
- HTML <map> 標簽
- HTML <mark> 標簽
- HTML <menu> 標簽
- HTML <menuitem> 標簽
- HTML <meta> 標簽
- HTML <meter> 標簽
- HTML <nav> 標簽
- HTML <noframes> 標簽
- HTML <noscript> 標簽
- HTML <object> 標簽
- HTML <ol> 標簽
- HTML <optgroup> 標簽
- HTML <option> 標簽
- HTML <output> 標簽
- HTML <p> 標簽
- HTML <param> 標簽
- HTML <pre> 標簽
- HTML <progress> 標簽
- HTML <q> 標簽
- HTML <rp> 標簽
- HTML <rt> 標簽
- HTML <ruby> 標簽
- HTML <s> 標簽
- HTML <script> 標簽
- HTML <section> 標簽
- HTML <select> 標簽
- HTML <small> 標簽
- HTML <source> 標簽
- HTML <span> 標簽
- HTML <strike> 標簽
- HTML <style> 標簽
- HTML <sub> 標簽
- HTML <summary> 標簽
- HTML <sup> 標簽
- HTML <table> 標簽
- HTML <tbody> 標簽
- HTML <td> 標簽
- HTML <textarea> 標簽
- HTML <tfoot> 標簽
- HTML <th> 標簽
- HTML <thead> 標簽
- HTML <time> 標簽
- HTML <title> 標簽
- HTML <tr> 標簽
- HTML <track> 標簽
- HTML <tt> 標簽
- HTML <u> 標簽
- HTML <ul> 標簽
- HTML <video> 標簽
- HTML <wbr> 標簽
- HTML 全局屬性
- HTML accesskey 屬性
- HTML class 屬性
- HTML contenteditable 屬性
- HTML contextmenu 屬性
- HTML data-* 屬性
- HTML dir 屬性
- HTML draggable 屬性
- HTML dropzone 屬性
- HTML hidden 屬性
- HTML id 屬性
- HTML lang 屬性
- HTML spellcheck 屬性
- HTML style 屬性
- HTML tabindex 屬性
- HTML title 屬性
- HTML translate 屬性
- HTML 事件屬性
- HTML 5 視頻/音頻參考手冊
- HTML 5 Canvas 參考手冊
- HTML 元素和有效的 DTD
- HTML 顏色名
- HTML 字符集
- HTML ASCII 參考手冊
- HTML ISO-8859-1 參考手冊
- HTML 4.01 符號實體
- HTML URL 編碼
- HTML 語言代碼參考手冊
- HTTP 狀態消息
- HTTP 方法:GET 對比 POST
- 免責聲明