Object name. AddEventListener ("event name (without on)", function name, true/false);(ff)
Object name. attachevent ("event name", function name);(IE)
Description
Event name, note that "onclick" to Change to "click", "onblur" to "blur", that is, the event name does not take "on".
Function name, remember not to have the last argument with parentheses is a Boolean value that represents the order of response for the event, and the following is an emphasis on the 3rd parameter (usecapture) of AddEventListener. If Usercapture is true, the browser uses the capture, or false to the Bubbing method. It is recommended to use false to see an example.
HTML code
<div id= "Div_test" > <input type= "button" id= "Btn_test" value= "se4.cn technical base"/> </div>
JS Code
Copy Code code as follows:
Window.onload=function () {document.getElementById ("Div_test"). AddEventListener ("click", Test1,false); document.getElementById ("Btn_test"). AddEventListener ("click", Test2,false); function Test1 () {alert ("outer div Trigger")} function Test2 () {alert ("Inner input Trigger")}
Experience it yourself, if Usercapture is true then test1 triggers first, if Usercapture is false test2 first.
Let's say, attachevent
This is not what to say, I believe we are all very familiar with, mainly to pass the block, and so I use it again, hahaha
Example:
To create a binding method:
Copy Code code as follows:
if (typeof Document.addeventlistener!= "undefined") {
Document.addeventlistener ("MouseDown", _lhlclick,true);
} else {
Document.attachevent ("onmousedown", _lhlclick);
}
To delete an event:
Copy Code code as follows:
if (typeof Document.addeventlistener!= "undefined") {
Document.removeeventlistener ("MouseDown", _lhlclick,true);
} else {
Document.detachevent ("onmousedown", _lhlclick);
}