About event bubbling and event delegation

Source: Internet
Author: User

Take the following code as an example:

<div>

<a></a>

</div>

Suppose there's a mouseout event on the Div.

In short, event bubbling is when the mouse is mouseout in the area of a, and the event is spread out from layer to layer, triggering the Mouseout event of the Div.

The event delegate is a feature that exploits event bubbling. Take the following code as an example:

<div id= "#switch" >

<button id= "BTN1" ></button>

<button id= "BTN1" ></button>

<button id= "BTN1" ></button>

</div>

Assuming that each button has a binding event response, it is not possible for each button to write $ ("Buttoni") if there are 10 buttons. Click (function () {

});

You can then delegate the event to the outer div by binding the Click event to the Div, such as the following code:

$ ("#switch"). Click (Function (event) {//event refers to the object that is actually clicked

if ((Event.target). Is (' button ') {//note. Using target to find the event target is often required to receive the innermost element of the event.

Determine what the ID is and implement the response

}

});

Here as a small supplement: Mouseout and MouseOver will have bubbling events, and MouseLeave and MouseEnter do not, only the specific specified elements will respond to the event.

About event bubbling and event delegation

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.