當用戶訪問你的網站時,他們會做一些事情,比如點擊各種連結,將滑鼠移到文本和圖像上等等。這些是我們在JavaScript和VBScript術語中稱之爲事件的例子。
我們可以使用JavaScript或VBScript編寫事件處理程序,並可以指定對這些事件採取的一些操作。雖然這些是事件,但它們將被指定爲HTML標記的屬性。
HTML 4.01規範定義了19個事件,但後來的HTML-5添加了許多其他事件,我們在下面列出了這些事件;
Window Events Attributes
在較舊版本的HTML中引入了以下事件,但所有標記爲的標記都是HTML-5的一部分。
Events | HTML-5 | Description |
---|---|---|
onafterprint | Triggers after a document is printed | |
onbeforeprint | Triggers before a document is printed | |
onbeforeonload | Triggers before a document loads | |
onerror | Triggers when an error occurs | |
onhaschange | Triggers when a document has changed | |
onload | Triggers when a document loads | |
onmessage | Triggers when a message is triggered | |
onoffline | Triggers when a document goes offline | |
ononline | Triggers when a document comes online | |
onpagehide | Triggers when a window is hidden | |
onpageshow | Triggers when a window becomes visible | |
onpopstate | Triggers when a window's history changes | |
onredo | Triggers when a document performs a redo | |
onresize | Triggers when a window is resized | |
onstorage | Triggers when a document loads | |
onundo | Triggers when a document performs an undo | |
onunload | Triggers when a user leaves the document |
Form Events
在較舊版本的HTML中引入了以下標記,但所有標記爲的標記都是HTML-5的一部分。
Events | HTML-5 | Description |
---|---|---|
onblur | Triggers when a window loses focus | |
onchange | Triggers when an element changes | |
oncontextmenu | Triggers when a context menu is triggered | |
onfocus | Triggers when a window gets focus | |
onformchange | Triggers when a form changes | |
onforminput | Triggers when a form gets user input | |
oninput | Triggers when an element gets user input | |
oninvalid | Triggers when an element is invalid | |
onreset | Triggers when a form is reset | |
onselect | Triggers when an element is selected | |
onsubmit | Triggers when a form is submitted |
Keyboard Events
Events | HTML-5 | Description |
---|---|---|
onkeydown | Triggers when a key is pressed | |
onkeypress | Triggers when a key is pressed and released | |
onkeyup | Triggers when a key is released |
Mouse Events
在較舊版本的HTML中引入了以下標記,但所有標記爲的標記都是HTML-5的一部分。
Events | HTML-5 | Description |
---|---|---|
onclick | Triggers on a mouse click | |
ondblclick | Triggers on a mouse double-click | |
ondrag | Triggers when an element is dragged | |
ondragend | Triggers at the end of a drag operation | |
ondragenter | Triggers when an element has been dragged to a valid drop target | |
ondragleave | Triggers when an element leaves a valid drop target | |
ondragover | Triggers when an element is being dragged over a valid drop target | |
ondragstart | Triggers at the start of a drag operation | |
ondrop | Triggers when a dragged element is being dropped | |
onmousedown | Triggers when a mouse button is pressed | |
onmousemove | Triggers when the mouse pointer moves | |
onmouseout | Triggers when the mouse pointer moves out of an element | |
onmouseover | Triggers when the mouse pointer moves over an element | |
onmouseup | Triggers when a mouse button is released | |
onmousewheel | Triggers when the mouse wheel is being rotated | |
onscroll | Triggers when an element's scrollbar is being scrolled |
Media Events
在較舊版本的HTML中引入了以下標記,但所有標記爲的標記都是HTML-5的一部分。