DOM allows us to write JS code to allow HTML elements to respond to events ;
events : interactions between users and web pages; eg: clicks on web pages;
monitoring : to allow the computer to detect that this event occurs at any time . Thereby executing some programs pre-written by programmers;
there are two main ways to set event listeners: onxxx and addEventListener() ;
to set their onxxx attributes ;
oBox.onclick = function () { // When the box is clicked, the statement here will be executed}
event name | event description |
---|---|
onclick | when the mouse is single on an object |
ondblclick | when the mouse double-clicks on an object |
onmousedown | when a mouse button is pressed on an object |
onmouseup | when a mouse button is released on an object |
onmousemove | when a mouse button is moved on an object |
onmouseenter | when the mouse enters an object (similar event onmouseover) |
onmouseleave | when the mouse leaves an object (similar event onmouseout) |
name | event description |
---|---|
onkeypress | when a keyboard key is pressed (system buttons such as arrow keys and function keys cannot be recognized) |
onkeydown | when a keyboard key is pressed (system buttons can be recognized, and will occur before onkeypress) |
onkeyup | When a keyboard key is released |
event name | Event description |
---|---|
onchange | When the user changes the content of a form field, it will trigger |
onfocus | When an element gains focus (such as the tab key or Mouse click) |
onblur | when an element loses focus |
onsubmit | when the form is submitted |
onreset | when the form is reset |
event name | event description |
---|---|
onload | when the page or image is completed loading |
onunload | when the user exits the page |
starts from the outside to the inside (capture phase) , and then from the inside to the outside (bubbling phase).
However, writing like onxxx (DOM level 0) can only monitor the bubbling phase ; so you need to use the addEventListener() method (DOM level 2);
oBox1.addEventListener ('click', function(){ // This is the event processing function}, true) // true means listening to the capture phase, false means listening to the bubbling phase.