JS Add the difference between event attachevent and AddEventListener

Source: Internet
Author: User

1, AddEventListener
The standard method for AddEventListener binding events, such as the following, in which the order of execution and binding order of events is consistent and the order of execution is method1->method2->method3

  1. //element.addEventListener(type,listener,useCapture);
  2. btn1Obj.addEventListener("click",method1,false);
  3. btn1Obj.addEventListener("click",method2,false);
  4. btn1Obj.addEventListener("click",method3,false);

2, Attachevent
In IE8 and the following versions, AddEventListener is not supported, only attachevent bound events, and the order of execution is method3->method2->method1

  1. //object.attachEvent(event,function);
  2. btn1Obj.attachEvent("onclick",method1);
  3. btn1Obj.attachEvent("onclick",method2);
  4. btn1Obj.attachEvent("onclick",method3);

3. Attribute Assignment method Binding event
Applying this method overrides the method that is bound later, to bind multiple at once (multiple operations can be performed at one time, multiple methods may be called in an event)

    1. obj.onclick=method1;
    2. obj.onclick=method2;
    3. obj.onclick=method3;

Bind multiple at once:

      1. obj.onclick=function (){
      2. method1();
      3. method2();
      4. method3();
      5. }

JS Add the difference between event attachevent and AddEventListener

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.