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", and "keydown.
Listener: implements the EventListener interface or functions in JavaScript.
UseCapture: whether to use capture. Generally, false is used. For example, document. getElementById ("testText"). addEventListener ("keydown", function (event) {alert (event. keyCode) ;}, false );
IE:
Target. attachEvent (type, listener );
Target: document node, document, window, or XMLHttpRequest.
Type: String, event name, including "on", such as "onclick", "onmouseover", and "onkeydown.
Listener: implements the EventListener interface or functions in JavaScript. For example, document. getElementById ("txt"). attachEvent ("onclick", function (event) {alert (event. keyCode );});
W3C and IE support removing specified events at the same time. The purpose is to remove the specified events in the following format:
W3C format:
RemoveEventListener (event, function, capture/bubble );
The format of Windows IE is as follows:
DetachEvent (event, function );
Target. addEventListener (type, listener, useCapture );
Target file node, document, window, or XMLHttpRequest.
Type string, event name, excluding "on", such as "click", "mouseover", and "keydown.
Listener implements the EventListener interface or functions in JavaScript.
UseCapture: whether to use capture. After reading the event stream section, you will understand it. Generally, false is used.
When an Event is triggered, an Event object is passed to the Event handler, for example:
Document. getElementById ("testText"). addEventListener ("keydown", function (event) {alert (event. keyCode) ;}, false );
Different browser versions are applicable.
The attachEvent method is used in onclick IE.
The addEventListener method is used in click fox.
The usage principle of the two is as follows: the priority of the execution can be different. The example below is as follows:
The attachEvent method attaches other processing events to an event. (Mozilla series not supported)
The addEventListener method is used in the Mozilla series.
Example: document. getElementById ("btn"). onclick = method1;
Document. getElementById ("btn"). onclick = method2;
Document. getElementById ("btn"). onclick = method3; if so, only medhot3 will be executed.
Write 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 sequence is method3-> method2-> method1
If the Mozilla series does not support this method, you need to use addEventListener 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); execution sequence: method1-> method2-> method3
Example: (Note that the div must be placed before the js)