A comprehensive understanding of the difference between AddEventListener and on _javascript tips

Source: Internet
Author: User

Why do you need AddEventListener?

Let's take a look at a fragment:

HTML code

<div id= "box" > Chasing the Dream Child </div>

With on code

Window.onload = function () {
  var box = document.getElementById ("box");
  Box.onclick = function () {
    Console.log ("I am Box1");
  }
  Box.onclick = function () {
    box.style.fontSize = "18px";
    Console.log ("I am Box2");
  }
Run Result: "I am Box2"

You see, the second onclick covers the first onclick, although most of the time we use on to get the results we want, but sometimes we need to execute multiple identical events, and obviously if we don't do what we want with on, then don't guess, you must know, yes! AddEventListener can bind the same event multiple times and will not overwrite the previous event.

Using the AddEventListener code

Window.onload = function () {
  var box = document.getElementById ("box");
  Box.addeventlistener ("click", Function () {
    Console.log ("I am Box1");
  })
  Box.addeventlistener ("click", Function () {
    Console.log ("I am Box2")
}
Run Result: I'm box1,
I'm box2.

Addeventlistenert method The first parameter fills in the event name, note that you do not need to write on, the second argument can be a function, and the third parameter refers to handling the event handler in the bubbling or capturing phase, if true represents capture-phase processing. If False represents bubbling phase processing, the third argument can be omitted, and most cases do not require a third parameter, not a third argument default false

Use of the third parameter

Sometimes this is the case.

<body>
<div id= "box" >
<div id= "Child" ></div>
</div>
</body>

If I add the Click event to box, what if I click on the box, but if I click the child element, how does it perform? (Order of execution)

Box.addeventlistener ("click", Function () {
  console.log ("box");
})

Child.addeventlistener ("click", Function () {
  console.log ("Child");
})
Result of execution:
child
Box

In other words, the default event events are performed in the order in which the event bubbles are executed.

If the third argument writes True, it is performed in the order in which the events are captured.

Box.addeventlistener ("click", Function () {
  console.log ("box"),
},true)

Child.addeventlistener ("  Click, function () {
  console.log ("Child");
})
Result of execution:
box
Child

Event Bubbling Execution Process:

Start bubbling up from the most specific element (the element you clicked), take our case above to say its order is: Child->box

Event Capture Execution Process:

From the least specific element (the outermost box) began to bubble inside, take our case above the order is: Box->child

The above comprehensive understanding of the difference between AddEventListener and on is small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.