AddEventListener () method
element. AddEventListener (event, function, Usecapture);
The first parameter is the type of event (such as "click" or "MouseDown").
The second parameter is a function that is called after the event is triggered.
The third parameter is a Boolean value that describes whether the event is bubbling or capturing. This parameter is optional.
Note: Do not use the "on" prefix. For example, use "click" instead of "onclick".
"Hello world!" pops up when the user clicks on an element:
element. AddEventListener ("click", Function () {alert ("Hello world!");}); You can use the function name to refer to an external function:
"Hello world!" pops up when the user clicks on an element:
element. AddEventListener ("click", MyFunction);
function MyFunction () {
Alert ("Hello world!");
When passing a parameter value, use an anonymous function to invoke the function with the parameter:
element. AddEventListener ("click", Function () {myFunction (P1, p2);});
The AddEventListener () method can specify the "usecapture" parameter to set the delivery type:
AddEventListener (
Event,
function,
usecapture);
The default is False, which is the bubbling pass, when the value is true, and the event uses capture delivery.
document.getElementById ("Mydiv"). AddEventListener ("Click", MyFunction, True); RemoveEventListener () method
The RemoveEventListener () method removes the event handle added by the AddEventListener () method:
Instance
element. RemoveEventListener ("MouseMove", myFunction);
Cross-Browser Workaround:
var x = document.getElementById ("mybtn");
if (X.addeventlistener) {//All major browsers except IE 8 and earlier
X.addeventlistener ("click", MyFunction);
} else if (x.attachevent) {///IE 8 and earlier versions
X.attachevent ("onclick", myFunction);
}
JS Event Monitoring