On the understanding of event binding and ordinary events in JS

Source: Internet
Author: User
Tags event listener

An ordinary event refers to an event that can be used for registration;

Event binding refers to registering an event on a specific element.

Popular Point says:

Normal event: Add a specific attribute (for example: onclick) to the HTML element;

Event binding: JS code gets the element by tag (ID tag Class), adding a specific method to the element ( for example: onclick);

Extended: Event listener AddEventListener event can bind multiple events and trigger

function (){};
<button onmouseover = " " ></button>


1, if the same element is bound two or more times the same type of event, then the subsequent binding will overwrite the previous binding
2. Dom Event Flow event capture Phase target element phase = Event Bubbling phase is not supported

AddEventListener
? If the same element is bound two or more times the same type of event, so the binding will be triggered sequentially
? Support for DOM event streams
The event binding parameter does not need to be on (in front of us)

AddEventListener ("click",function() {},true);   // the event at this point is performed during the event bubbling phase

Compatibility processing

IE9 began to Ie11 Edge:addeventlistener

IE9 before: attachevent/detachevent

? Event type parameters need to be prefixed with on
? This approach only supports event bubbling and does not support event capture

Compare attachevent and AddEventListener:

    • Attachevent only supports event bubbling AddEventListener supports both event bubbling and event capture
    • Parameter: Attachevent event type requires on prefix, AddEventListener event type does not need an on prefix
    • If you use Attachevent to bind multiple events to the target stage of an element, it is triggered in the reverse order of the binding order, and if you use AddEventListener to bind multiple events to the target stage of an element, it is triggered in the binding order

On the understanding of event binding and ordinary events in JS

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.