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
<%@ page language= "C #" autoeventwireup= "true" codefile= " Default5.aspx.cs "inherits=" Default5 "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
<title>porschev---Jquery event bubbling </title>
<script src= "Jquery-1.3.2-vsdoc.js" type= "Text/javascript" ></SCRIPT>
<body>
<form id= "Form1" runat= "Server",
<div id= "Divone" onclick= "alert (' I am the outermost ');"
<div id= "Divtwo" onclick= "alert (' I'm the middle tier!
<a id= "Hr_three" href= "http://www.baidu.com" mce_href= "http://www.baidu.com" onclick= "alert (' I am the innermost! ') > click on my </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 layer----> I am the outermost
----> 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 type= "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.return false;
If the header is joined by the following code
<script type= "Text/javascript" >
$ (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
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.return false;
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)