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

Source: Internet
Author: User

Original address: http://www.jb51.net/article/32792.htm

What is the JS event bubbling?
Fires a class of events on an object (such as clicking the OnClick event), and if this object defines a handler for this event, then this event invokes the handler, and if the event handler is not defined or the event returns true, the event propagates to the object's parent object, from inside to outside, Until it is processed (all the same events of the parent object will be activated), or it reaches the topmost level of the object, the Document object (some browsers are windows).

How do I prevent jquery events from bubbling?
By a small example to explain

Copy CodeThe code is as follows:
<% @PageLanguage = "C #" autoeventwireup= "true" codefile= "Default5.aspx.cs" inherits= "DEFAULT5"%>
<! Doctypehtmlpublic "-//w3c//dtdxhtml1.0transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>porschev---jquery event bubble </title>
<scriptsrc= "Jquery-1.3.2-vsdoc.js" type= "Text/javascript" ></script>
<body>
<formid= "Form1" runat= "Server" >
<divid= "Divone" onclick= "alert (' I am the outermost ');" >
<divid= "Divtwo" onclick= "alert (' I am the middle tier! ') ' >
<aid= "Hr_three" href= "http://www.baidu.com" mce_href= "http://www.baidu.com" onclick= "alert (' I'm the innermost! ') ' > click on Me </a>
</div>
</div>
</form>
</body>


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 ();

Copy CodeThe code is as follows:
<scripttype= "Text/javascript" >
$ (function () {
$ ("#hr_three"). Click (Function (event) {
Event.stoppropagation ();
});
});
<script>


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

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

Copy CodeThe code is as follows:
<scripttype= "Text/javascript" >
$ (function () {
$ ("#hr_three"). Click (Function (event) {
Returnfalse;
});
});
<script>


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

From This we can see :
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.