取消html中onclick的事件冒泡

   阻止html中onclick事件冒泡,处理方法上多加事件一个参数,不需要传值。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <meta charset="utf-8" />
   
</head>
<body>
    <div style='background:red;width:200px;height:50px' onclick='a()'>
        <input type='button' value='ok' onclick='b()'></input>
    </div>
   <script type="text/javascript">
     function a(e){
        alert("this div")
     }
      function b(e){
        alert("this button")
        if(e&&e.stopPropagation){
            e.stopPropagation();
        }else{
        window.event.cancelBubble =true;
        }
     }
    </script>
</body>
</html>
View Code

结果:阻止,button按钮上的click事件冒泡

猜你喜欢

转载自www.cnblogs.com/licorice/p/10171713.html