JavascriptattachEvent and addEventListener usage _ javascript skills

Source: Internet
Author: User
The differences between attachEvent and addEventListener adapt to different browser versions. during use, pay attention to the onclick button of the attachEvent method.
AddEventListener method button click
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:
Java code

The Code is as follows:


Document. getElementById ("btn"). onclick = method1;
Document. getElementById ("btn"). onclick = method2;
Document. getElementById ("btn"). onclick = method3;


If this is the case, only medhot3 will be executed.
Write as follows:
Java code:

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 sequence is method3-> method2-> method1
If the Mozilla series does not support this method, you need to use addEventListener.
Java code:

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 sequence is method1-> method2-> method3
Instance used:
1. Java code:

The Code is as follows:


Var el = EDITFORM_DOCUMENT.body;
// Get the object first. EDITFORM_DOCUMENT is actually an iframe
If (el. addEventListener )...{
El. addEventListener ('click', KindDisableMenu, false );
} Else if (el. attachEvent )...{
El. attachEvent ('onclick', KindDisableMenu );
}


2. Java code:

The Code is as follows:


If (window. addEventListener ){
Window. addEventListener ('load', _ uCO, false );
} Else if (window. attachEvent ){
Window. attachEvent ('onload', _ uCO );
}

Related Article

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.