Home >Web Front-end >JS Tutorial >Detailed explanation of the use of addEventListener in JavaScript
addEventListener is used to register the event processing program, which is attachEvent in IE. Why do we talk about addEventListener instead of attachEvent? Firstly, attachEvent is relatively simple, and secondly, addEventListener is the standard content in DOM. Today I will introduce to you a detailed explanation of the use of addEventListener in JavaScript!
(It should be noted that p must be placed in front of js)
Generally, if you bind the same event to a DOM object, only the last one will take effect. For example:
The code is as follows:
document.getElementById("btn").onclick = method1; document.getElementById("btn").onclick = method2; document.getElementById("btn").onclick = method3;
Then only method3 will take effect.
If it is the Mozilla series, use addEventListener to implement multiple events in order, for example:
The code is as follows:
var btn1Obj = document.getElementById("btn1"); //element.addEventListener(type,listener,useCapture); btn1Obj.addEventListener("click",method1,false); btn1Obj.addEventListener("click",method2,false); btn1Obj.addEventListener("click",method3,false);
The execution order is method1->method2- >method3
If it is an IE series, you can use attachEvent to implement multiple events in order, for example:
The code is as follows:
var btn1Obj = document.getElementById("btn1"); //object.attachEvent(event,function); btn1Obj.attachEvent("onclick",method1); btn1Obj.attachEvent("onclick",method2); btn1Obj.attachEvent("onclick",method3);
The execution order is method3->method2->method1
======================================== =================
In Mozilla:
How to use addEventListener
target.addEventListener(type,listener,useCapture);
target: document node, document, window or XMLHttpRequest .
type: String, event name, excluding "on", such as "click", "mouseover", "keydown", etc.
listener: Implements the EventListener interface or a function in JavaScript.
useCapture: Whether to use capture, usually false. For example:
document.getElementById("testText").addEventListener("keydown", function (event) { alert(event.keyCode); }, false);
In IE:
target.attachEvent(type, listener);
target: document node, document, window or XMLHttpRequest.
type: String, event name, including "on", such as "onclick", "onmouseover", "onkeydown", etc.
listener: Implements the EventListener interface or a function in JavaScript. For example: document.getElementById("txt").attachEvent("onclick",function(event){alert(event.keyCode);});
W3C and IE both support the removal of specified events. The purpose is to remove The formats of the set events are as follows:
removeEventListener(event,function,capture/bubble);
The format of Windows IE is as follows:
detachEvent(event,function);
The evolution of DOM2:
DOM 0 Event | DOM 2 Event |
blur | |
focus | |
change | |
mouseover | |
mouseout | |
mousemove | |
mousedown | onmouseup() |
onclick() | |
dblclick | onkeydown() |
onkeyup() | |
onkeypress() | |
onsubmit() | |
onload() | |
onunload() | |
The above is the detailed content of Detailed explanation of the use of addEventListener in JavaScript. For more information, please follow other related articles on the PHP Chinese website!