JQuery blocks event bubbling, and jquery blocks bubbling.

Source: Internet
Author: User

JQuery blocks event bubbling, and jquery blocks bubbling.

During our usual development process, we will certainly encounter a situation where a div (this div can be an element) wraps a div. However, events are added to both Divs, if you click the div in it, we want to handle the div event. However, we do not want the div event to be executed on the outer layer. At this time, we need to block the bubble.

In other words, you watch TV at home and hide in your own small room, but you don't want your voice to be passed to the ears of your parents next door. At this time, you may be hiding in your bed, or the sound insulation effect on the walls is good. Blocking sound can be understood as blocking bubbles.

Three methods to prevent event bubbles

1. return false: the default events and bubble events can be blocked.

2. event. cancelBubble = true under event. stopPropagation/IE;: the event can be blocked but the default event is allowed.

3. event. preventDefault ();/event. returnValue = false in IE: the default event can be blocked, but the bubble event is allowed.

The above three methods can be used properly in different scenarios. below is the code and you can perform some tests on your own:

<! DOCTYPE html> 

 

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.