English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The value of the event handler is one or a series of JavaScript expressions, methods, and function calls separated by semicolons, and enclosed in quotes. When an event occurs, the browser will execute this code.
HTML 4 One of the new features is that it can enable HTML events to trigger behaviors in the browser, such as starting a JavaScript script when a user clicks on an HTML element.
The following table provides standard event attributes that can be inserted into HTML/In XHTML elements, to define event behavior.
HTML5 : HTML5New event attributes.
The event is triggered by the window (for the <body> tag):
Attribute | Value | Description |
onafterprintHTML5 | script | Run the script after the document is printed |
onbeforeprintHTML5 | script | Run the script before the document is printed |
onbeforeonloadHTML5 | script | Run the script before the document is loaded |
onblur | script | Run the script when the window loses focus |
onerrorHTML5 | script | Run the script when an error occurs |
onfocus | script | Run the script when the window receives focus |
onhaschangeHTML5 | script | Run the script when the document changes |
onload | script | Run the script when the document is loaded |
onmessageHTML5 | script | Run the script when a message is triggered |
onofflineHTML5 | script | Run the script when the document goes offline |
ononlineHTML5 | script | Run the script when the document goes online |
onpagehideHTML5 | script | Run the script when the window is hidden |
onpageshowHTML5 | script | Run the script when the window is visible |
onpopstateHTML5 | script | Run the script when the window history changes |
onredoHTML5 | script | Run the script when the document performs a redo operation |
onresizeHTML5 | script | Run the script when the window size is adjusted |
onstorageHTML5 | script | Run the script when the Web Storage area is updated (when data in the storage space changes) |
onundoHTML5 | script | Run the script when the document performs an undo operation |
onunloadHTML5 | script | Run the script when the user leaves the document |
Form events are triggered in HTML forms (applicable to all HTML elements, but the HTML element must be within the form):
Attribute | Value | Description |
onblur | script | Run the script when an element loses focus |
onchange | script | Run the script when an element changes |
oncontextmenuHTML5 | script | Run the script when the context menu is triggered |
onfocus | script | Run the script when an element receives focus |
onformchangeHTML5 | script | Run the script when the form changes |
onforminputHTML5 | script | Run the script when the form receives user input |
oninputHTML5 | script | Run the script when an element receives user input |
oninvalidHTML5 | script | Run the script when an element is invalid |
onreset | script | Run the script when the form is reset. HTML 5 Not supported. |
onselect | script | Run the script when an element is selected |
onsubmit | script | Run the script when the form is submitted |
Attribute | Value | Description |
onkeydown | script | Run the script when the key is pressed |
onkeypress | script | Run the script when the key is pressed and released |
onkeyup | script | Run the script when the key is released |
Triggered by mouse events, similar to user actions:
Attribute | Value | Description |
onclick | script | When clicking the mouse, run the script |
ondblclick | script | When double-clicking the mouse, run the script |
onmousedown | script | The script runs when the mouse button is pressed |
onmousemove | script | The script runs when the mouse pointer moves |
onmouseout | script | The script runs when the mouse pointer moves out of the element |
onmouseover | script | The script runs when the mouse pointer is moved over the element |
onmouseup | script | The script runs when the mouse button is released |
onmousewheelHTML5 | script | The script runs when the mouse wheel is scrolled |
Triggered by dragging the mouse, similar to user behavior:
Attribute | Value | Description |
ondragHTML5 | script | The script runs when a draggable element is being dragged |
ondragendHTML5 | script | The script runs when the drag operation ends |
ondragenterHTML5 | script | The script runs when the element is dropped onto a valid drag-and-drop target |
ondragleaveHTML5 | script | The script runs when the element leaves a valid drag-and-drop target |
ondragoverHTML5 | script | The script runs when the element is dragged over a valid drag-and-drop target |
ondragstartHTML5 | script | The script runs when the drag operation starts |
ondropHTML5 | script | The script runs when a draggable element is being dragged and dropped |
onscrollHTML5 | script | The script runs when the scrollbar of a scrolling element is scrolled |
Triggered by video (videos), images (images), or audio (audio), commonly used in HTML media elements like <audio>, <embed>, <img>, <object>, and <video>):
Attribute | Value | Description |
onabort | script | The script runs when an abort event occurs |
oncanplayHTML5 | script | The script runs when the media can start playing but may need to stop due to buffering |
oncanplaythroughHTML5 | script | The script runs when the media can be played to the end without stopping due to buffering |
ondurationchangeHTML5 | script | The script runs when the media length changes |
onemptiedHTML5 | script | The script runs when the media resource element becomes unexpectedly empty (due to network errors, loading errors, etc.) |
onendedHTML5 | script | The script runs when the media has reached the end |
onerrorHTML5 | script | The script runs when an error occurs during the loading of the element |
onloadeddataHTML5 | script | The script runs when loading media data |
onloadedmetadataHTML5 | script | The script runs when the duration of the media element and other media data has been loaded |
onloadstartHTML5 | script | The script runs when the browser starts loading media data |
onpauseHTML5 | script | The script runs when the media data is paused |
onplayHTML5 | script | The script runs when the media data is about to start playing |
onplayingHTML5 | script | The script runs when the media data has started playing |
onprogressHTML5 | script | The script runs while the browser is fetching media data |
onratechangeHTML5 | script | Script runs when the playback rate of the media data changes |
onreadystatechangeHTML5 | script | Script runs when the ready state (ready-state) changes |
onseekedHTML5 | script | Script runs when the media element's positioning attribute [1No longer true and positioning has ended |
onseekingHTML5 | script | Script runs when the media element's positioning attribute is true and positioning has started |
onstalledHTML5 | script | Script runs when there is an error in the process of fetching media data (delayed) |
onsuspendHTML5 | script | Script runs when the browser is already fetching media data but has stopped before retrieving the entire media file |
ontimeupdateHTML5 | script | Script runs when the media changes its playback position |
onvolumechangeHTML5 | script | Script runs when the volume of the media changes or when the volume is set to mute |
onwaitingHTML5 | script | Script runs when the media has stopped playing but intends to continue playing |
Attribute | Value | Description |
onshowHTML5 | script | Triggered when the <menu> element is displayed in context |
ontoggleHTML5 | script | Triggered when the user opens or closes the <details> element |