JavaScript attachevent and AddEventListener using methods _javascript Tips

Source: Internet
Author: User
Attachevent Method Button onclick
AddEventListener Method Button click
The rationale for the use of the two is that it is not the same as the priority of execution, as the following examples explain:
Attachevent method, attaching additional handling events for an event. (Mozilla series not supported)
AddEventListener Method for Mozilla series
Example:
Java code
Copy Code code as follows:

document.getElementById ("btn"). onclick = method1;
document.getElementById ("btn"). onclick = Method2;
document.getElementById ("btn"). onclick = method3;

If you write this, then only MEDHOT3 will be executed.
Written in this way:
Java code:
Copy Code code as follows:

var btn1obj = document.getElementById ("btn1");
Object.attachevent (event,function);
Btn1obj.attachevent ("onclick", method1);
Btn1obj.attachevent ("onclick", method2);
Btn1obj.attachevent ("onclick", method3);

The order of execution is method3->method2->method1
If it is a Mozilla series, this method is not supported and requires the use of AddEventListener
Java code:
Copy Code code 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 order of execution is method1->method2->method3
Use instance:
1. Java code:
Copy Code code as follows:

var el = editform_document.body;
First get the object, Editform_document is an IFRAME
if (El.addeventlistener) ... {
El.addeventlistener (' Click ', Kinddisablemenu, false);
else if (el.attachevent) ... {
El.attachevent (' onclick ', kinddisablemenu);
}

2. Java code:
Copy Code code 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.