Differences between attachEvent and addEventListener

Source: Internet
Author: User

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 JavaSFunctions in callback.
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.
Attachevent method button 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 you write in this way, 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)



<Div id = "name1" style = "border: 1px solid red; padding: 10px 10px 10px 10px;">
<Div id = "name2" style = "border: 1px solid green; padding: 10px 10px 10px 10px;"> click </div>
<Div id = "info"> </div>
Var name1 = document. getElementById ('name1'); // note
Var name2 = document. getElementById ('name2'); // note
Var info = document. getElementById ('info ');
If (name1.attachEvent) {// we must ensure that the target in front of attachEvent is not empty.
Name1.attachEvent ('onclick', function () {info. innerHTML + = "red" + "<br> ";});
Name2.attachEvent ('onclick', function () {info. innerHTML + = "green" + "<br> ";});
} Else {
Name1.addEventListener ('click', function () {info. innerHTML + = "red" + "<br>" ;}, false );
Name2.addEventListener ('click', function () {info. innerHTML + = "green" + "<br>" ;}, false );



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.