How does jQuery prevent event bubbling?

Source: Internet
Author: User
Condition: the outer layer has a DIV element and the inner layer has a P element. The relationship exists as follows: DIV is the parent element of the P element, while P is the child element of the outer DIV, there is a relationship between the two. Condition: the outer layer has a DIV element and the inner layer has a P element. The relationship exists as follows: DIV is the parent element of the P element, while P is the child element of the outer DIV, there is a relationship between the two.

Event: now we are all bound to the same event on these two elements, such as the click event.

Result: When we click the P tag of the inner layer, the click Event of the inner layer element is triggered, and the click Event of the outer DIV is also triggered.

JQuery provides three methods to prevent event bubbles.

Method 1: event. stopPropagation ();

Method 2: event. preventDefault ();

Method 3: return false;

Difference: event. stopPropagation () only blocks events from bubbling up and does not block the event itself. Event. preventDefault () only blocks the event itself and does not prevent the event from bubbling up. Return false not only prevents the event from bubbling up, but also blocks the event itself.

Related Article

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.