<div class="codetitle"> <span><a style="CURSOR: pointer" data="49473" class="copybut" id="copybut49473" onclick="doCopy('code49473')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code49473"> <br><!DOCTYPE HTML> <br><html> <br><head> <br><title>test</title> <br><script src="http://code.jquery.com/jquery-1.9.1.min.js">< /script> <br></head> <br><body> <br><div style="height:300px;background-color:red;" id="red"> <br>< div style="height:200px;background-color:green;" id="green"> <br></div> <br></div> <br></body> <br>< ;/html> <br> </div> <br>In the above HTML, the red element is the parent element of the green element. <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="41187" class="copybut" id="copybut41187" onclick="doCopy('code41187')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code41187"> <br>$(function(){ <br>$(" #green").click(function(event){ <br>alert("green click1"); <br>}); <br>$("#green").click(function(event){ <br> alert("green click2"); <br>}); <br>$("#red").click(function(event){ <br>alert("red click"); <br>}); <br>}); <br> </div> <br>The above js code adds three click events to the red element and green element. <br>When the sub-element green element is clicked, green click1, green click2, and red click events will be executed in sequence. <br>Event addition: Clicking on the green element will execute two events, green click1 and green click2 (events of the same type on the same element) in sequence. <br>Event bubbling: Clicking on the green element will trigger the red click event of the parent element (the same type of event of the parent element). <br>1. Prevent event bubbling in the child element event function <br>Method 1: event.stopPropagation() <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="19160" class="copybut" id="copybut19160" onclick="doCopy('code19160')"><u>Copy code </u></a></span> The code is as follows: </div> <div class="codebody" id="code19160"> <br>$(function(){ <br>$("#green").click(function(event){ <br>event.stopPropagation(); <br> alert("green click"); <br>}); <br>$("#red").click(function(event){ <br>alert("red click"); <br>}); <br>}); <br> </div> <br>Method 2: return false <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="58681" class="copybut" id="copybut58681" onclick="doCopy('code58681')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code58681"> <br>$(function(){ <br>$("#green").click(function(event){ <br>alert("green click"); <br>return false; <br>}); <br>$("#red").click(function(event){ <br>alert("red click"); <br>}); <br>}); <br> </div> <br>Through the above two methods, after clicking the green area of the sub-element, the red click event is blocked and no longer executed. But it will not affect clicks in other areas of the red element. <br>The difference between the two: <br>return false is equivalent to event.preventDefault() event.stopPropagation(). <br>2. Prevent event bubbling in the parent element event function <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="40119" class="copybut" id="copybut40119" onclick="doCopy('code40119')"><u>Copy the code </u></a></span> The code is as follows: </div> <div class="codebody" id="code40119"> <br>$(function(){ <br>$("#green").click(function(event){ <br>alert("green click"); <br>}); <br>$(" #red").click(function(event){ <br>if(event.target == this) <br>{ <br>alert("red click"); <br>} <br>}); <br>}); <br> </div> <br>Use if(event.target== this) to determine whether the clicked target element is the red element itself. If it is not red itself but its sub-element green element, then no The code inside the if will be executed. <br>3. Prevent event appending <br>The above method can only prevent event bubbling (that is, events of the same type on the parent element), but cannot prevent event appending (events of the same type on the same element). <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="22365" class="copybut" id="copybut22365" onclick="doCopy('code22365')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code22365"> <br>$(function(){ <br>$(" #green").click(function(event){ <br>event.stopImmediatePropagation(); <br>alert("green click"); <br>}); <br>$("#green").click (function(){ <br>alert("green click2"); <br>}); <br>}); <br> </div> <br>event.stopImmediatePropagation() can not only prevent the green click2 event, but also Also prevents events from bubbling up.