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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM