取消html中onclick的事件冒泡

   阻止html中onclick事件冒泡,處理方法上多加事件一個參數,不須要傳值。javascript

<!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事件冒泡,前端事件以下,或者如圖阻止事件冒泡html

 

相關文章
相關標籤/搜索