Introduction to jquery event bubbling and how to prevent events from bubbling

Source: Internet
Author: User

What is the JS event bubbling?  

How do I prevent jquery events from bubbling?  

<! DOCTYPE html>

such as the above page,
divided into three layers: Divone is the outer layer, divtwo middle layer, hr_three is the innermost layer;
they all have their own click events, and the innermost a tag has an href attribute.
run the page, click "Click Me", will pop up: I am the innermost----> I am the middle tier----> I am the outermost----> and then link to Baidu.
This is the event bubbling, originally I only clicked the tag with ID Hr_three, but did perform three alert operations.
event bubbling process (denoted by tag ID): hr_three---->divtwo---->divone. Bubbles from the innermost layers to the outermost layer.

How to stop it?  
1.event.stoppropagation ();

<script> $ (function () {$ ("#hr_three"). Click (Function (event) {event.stoppropagation ();     }); }); <script>

then click "Click Me", will pop up: I am the innermost layer, and then link to Baidu  

2.returnfalse; 
If the header is joined by the following code  

<script "> $ (Function () {$ (" #hr_three "). Click (Function (event) {return false;     }); }); <script>


then click on "Click Me" will pop up: I am the innermost, but will not execute link to Baidu page

This can be seen:
1.event.stoppropagation ();
Event bubbling is blocked during event processing, but does not halt the default behavior (it performs a hyperlink jump)
2.returnfalse;
Event bubbling is blocked during event processing, and the default behavior is also blocked (for example, a jump that did not perform a hyperlink just now)
There is also a bubbling-related:
3.event.preventdefault ();
If you put it in the Click event of the head a tag, click "Click Me".
will find it pop up in turn: I am the innermost----> I am the middle tier----> I am the outermost, but finally did not jump to Baidu
its role is: During event processing, do not halt the event bubbling, but the default behavior (it only executes all the bullets, but did not perform a hyperlink jump)

Introduction to jquery event bubbling and how to prevent events from bubbling

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.