JS Event Monitoring Js:attachevent and AddEventListener use method

Source: Internet
Author: User

the difference between attachevent and AddEventListener
Adapt to different browser versions, while using the process to pay attention to
Attachevent Method Button onclick
AddEventListener Method Button click

The principle of use: You can operate on events that do not have the same priority:
The Attachevent method, which attaches additional processing events to an event. (Mozilla series not supported)

AddEventListener Method for Mozilla series

Example:

JS Code
    • document.getElementById ("btn"). onclick = method1;
    • document.getElementById ("btn"). onclick = Method2;
    • document.getElementById ("btn"). onclick = method3; If this is the case, then only MEDHOT3 will be executed.

Written like this:

JS Code
    • var btn1obj = document.getElementById ("Btn1″"); Object.attachevent (event,function);
    • Btn1obj.attachevent ("onclick", method1);
    • Btn1obj.attachevent ("onclick", method2);
    • Btn1obj.attachevent ("onclick", method3); execution order is method3->method2->method1

In the case of the Mozilla series, this method is not supported and needs to be 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 order is method1->method2->method3

Usage examples:

JS Code
    • 1. var el = editform_document.body;
    • First get the object, Editform_document is actually an IFRAME
    • if (El.addeventlistener) {
    • El.addeventlistener (' Click ', Kinddisablemenu, false);
    • } else if (el.attachevent) {
    • El.attachevent (' onclick ', kinddisablemenu);
    • }2. if (Window.addeventlistener) {
    • Window.addeventlistener (' Load ', _uco, false);
    • } else if (window.attachevent) {
    • Window.attachevent (' onload ', _uco);
    • }

DetachEvent and RemoveEventListener

JS Code
    • DetachEvent (' onclick ', func);//ie using Delete event func
    • RemoveEventListener (' click ', func);//mozilla, delete event func

From:http://blog.163.com/[email protected]/blog/static/288964142010755811775/

JS Event Monitoring Js:attachevent and AddEventListener use method

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.