SoFunction
Updated on 2025-03-04

Prevent JavaScript events from bubble passing (cancelBubble, stopPropagation)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> 
<html xmlns="http:///1999/xhtml" lang="gb2312"> 
<head> 
<title> Stop the bubble passing of JavaScript events (cancelBubble , stopPropagation)</title>
<meta name="keywords" content="JavaScript, event bubbles, cancelBubble, stopPropagation" />
<script type="text/javascript"> 
function doSomething (obj,evt) { 
 alert(); 
 var e=(evt)?evt:; 
 if () { 
 =true; 
 } else { 
 //(); 
 (); 
 } 

</script> 
</head> 
<body> 
<div  onclick="alert()" style="width:250px;background-color:yellow"> 
 <p>This is parent1 div.</p> 
 <div  onclick="alert()" style="width:200px;background-color:orange"> 
 <p>This is child1.</p> 
 </div> 
 <p>This is parent1 div.</p> 
</div> 
<br /> 
<div  onclick="alert()" style="width:250px;background-color:cyan;"> 
 <p>This is parent2 div.</p> 
 <div  onclick="doSomething(this,event);" style="width:200px;background-color:lightblue;"> 
 <p>This is child2. Will bubble.</p> 
 </div> 
 <p>This is parent2 div.</p> 
</div> 
</body> 
</html>