HTML 元素可擁有事件屬性,這些屬性在瀏覽器中觸發行為,比如當用戶單擊一個 HTML 元素時啟動一段 JavaScript。下面列出的事件屬性,可以把它們插入 HTML 標簽來定義事件行為。
HTML 5 中的新事件:onabort, onbeforeunload, oncontextmenu, ondrag, ondragend, ondragenter, ondragleave, ondragover, ondragstart, ondrop, onerror, onmessage, onmousewheel, onresize, onscroll, onunload。
不再支持的 HTML 4.01 屬性:onreset。
| 屬性 | 值 | 描述 | 4 | 5 |
|---|---|---|---|---|
| onabort | script | 發生 abort 事件時運行腳本。 | 5 | |
| onbeforeonload | script | 在元素加載前運行腳本。 | 5 | |
| onblur | script | 當元素失去焦點時運行腳本。 | 4 | 5 |
| onchange | script | 當元素改變時運行腳本。 | 4 | 5 |
| onclick | script | 在鼠標點擊時允許腳本。 | 4 | 5 |
| oncontextmenu | script | 當菜單被觸發時運行腳本。 | 5 | |
| ondblclick | script | 當鼠標雙擊時運行腳本。 | 4 | 5 |
| ondrag | script | 只要腳本在被拖動就允許腳本。 | 5 | |
| ondragend | script | 在拖動操作結束時運行腳本。 | 5 | |
| ondragenter | script | 當元素被拖動到一個合法的放置目標時,執行腳本。 | 5 | |
| ondragleave | script | 當元素離開合法的放置目標時。 | 5 | |
| ondragover | script | 只要元素正在合法的放置目標上拖動時,就執行腳本。 | 5 | |
| ondragstart | script | 在拖動操作開始時執行腳本。 | 5 | |
| ondrop | script | 當元素正在被拖動時執行腳本。 | 5 | |
| onerror | script | 當元素加載的過程中出現錯誤時執行腳本。 | 5 | |
| onfocus | script | 當元素獲得焦點時執行腳本。 | 4 | 5 |
| onkeydown | script | 當按鈕按下時執行腳本。 | 4 | 5 |
| onkeypress | script | 當按鍵被按下時執行腳本。 | 4 | 5 |
| onkeyup | script | 當按鈕松開時執行腳本。 | 4 | 5 |
| onload | script | 當文檔加載時執行腳本。 | 4 | 5 |
| onmessage | script | 當 message 事件觸發時執行腳本。 | 5 | |
| onmousedown | script | 當鼠標按鈕按下時執行腳本。 | 4 | 5 |
| onmousemove | script | 當鼠標指針移動時執行腳本。 | 4 | 5 |
| onmouseover | script | 當鼠標指針移動到一個元素上時執行腳本。 | 4 | 5 |
| onmouseout | script | 當鼠標指針移出元素時執行腳本。 | 4 | 5 |
| onmouseup | script | 當鼠標按鈕松開時執行腳本。 | 4 | 5 |
| onmousewheel | script | 當鼠標滾輪滾動時執行腳本。 | 5 | |
| onreset | script | 當表單重置時執行腳本。不支持。 | 4 | |
| onresize | script | 當元素調整大小時運行腳本。 | 5 | |
| onscroll | script | 當元素滾動條被滾動時執行腳本。 | 5 | |
| onselect | script | 當元素被選中時執行腳本。 | 4 | 5 |
| onsubmit | script | 當表單提交時運行腳本。 | 4 | 5 |
| onunload | script | 當文檔卸載時運行腳本。 | 5 |
新聞熱點
疑難解答