What is bubbling
Simply put, triggers a child container event, and the parent container event is also triggered.
<div id= "Parentdiv" onclick= "alert" (' Parent '); > Parent
<div id= "Childdiv" onclick= "alert" (' child '); >child</div>
</div>
Alert (' child ') and alert (' parent ') are added separately on child and parent, and when we click on the child, the alert (' child ') is executed first, and then the parent element's alert (' Parent ') will also be executed, of course, if there are more levels, the parent of the event will be triggered in turn, this is bubbling.
But sometimes I do not need such a mechanism, as we click on the child only to trigger the child's alert (' Child ') event, then we have to prevent the occurrence of bubbles, the practice is as follows.
How do I stop bubbles?
There are two ways to block bubbles
E.cancelbubble=true;
E.stoppropagation ();
It is said that E.stoppropagation () is for Firefox, E.cancelbubble=true is for IE.
Here's an example
<div id= "Parentdiv" onclick= "alert" (' Parent '); >
Parent
<div id= "Childdiv" onclick= "dosomething (this,event);" >child</div>
</div>
function DoSomething (obj,evt) {
var e=evtwindow.event;
E.stoppropagation ();
}
The alert (' Parent ') event on the Parentdiv is not triggered, because it prevents bubbling in the dosomething.
How to use bubbles?
Of course, sometimes we use bubbles, meet our needs, for example, there are many elements to add an event to deal with something, but if you add an element to the onclick, first performance does not say, so much code will be ridiculed, this can be used to bubble.
Because the triggers of these elemental events can be bubbling to trigger his father's events, just add an incident to his father, and then judge the exact time of the element to be triggered. Then you can do whatever you want.
Example:
<table onclick= "CLICKTD (event);" Width= "height=" border= "1" >
<tr>
<TD id= "TD1" width= "25%" >td1</td>
<TD id= "TD2" width= "25%" >td2</td>
<TD id= "td3" width= "25%" >td3</td>
<TD id= "td4" width= "25%" >td4</td>
</tr>
</table>
function Clicktd (e) {
e = e window.event;
var obj = E.target e.srcelement;
alert (obj.id);
}
This is mainly done by e.target or e.srcelement (depending on the browser) to get the exact element. You should know what to do next.
Finally, a collection of examples
<title>www.cxybl.com</title>
<style>
#parentDiv {width:200px;height:200px;background: #666;}
#childDiv {width:100px;height:100px;background: #06C; margin:50px;}
</style>
<body>
Not prevented from bubbling:
<div id= "Parentdiv" onclick= "alert" (' Parent '); >
Parent
<div id= "Childdiv" onclick= "alert (' child ')" >child</div>
</div>
<br/>
To be prevented from bubbling:
<div id= "Parentdiv" onclick= "alert" (' Parent '); >
Parent
<div id= "Childdiv" onclick= "dosomething (this,event);" >child</div>
</div>
<br/>
Bubbling applications:
<table onclick= "CLICKTD (event);" Width= "height=" border= "1" >
<tr>
<TD id= "TD1" width= "25%" >td1</td>
<TD id= "TD2" width= "25%" >td2</td>
<TD id= "td3" width= "25%" >td3</td>
<TD id= "td4" width= "25%" >td4</td>
</tr>
</table>
<script>
function DoSomething (obj,evt) {
var e=evtwindow.event;
Alert ("Child");
E.stoppropagation ();
}
function Clicktd (e) {
EE = e window.event;
var obj = E.target e.srcelement;
alert (obj.id);
}
</script>
</body>