zoukankan      html  css  js  c++  java
  • js 阻止冒泡和默认行为

    js 阻止冒泡和默认行为

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>标题</title>
    <style type="text/css">
    *{margin:0;padding:0;}
    .wrapper{display:block;100px;height:100px;margin:0 auto;border:1px solid red;}
    </style>
    </head>
    <body>
    <a href="#" target="_blank" class="wrapper" id="wrapper"></a>
    <script type="text/javascript">
    window.onload = function(){
        var oWrapper = getId("wrapper");
        oWrapper.onclick = function(e){
            var oEvent = e || event;
            if(oEvent.stopPropagation){  //firefox
                oEvent.stopPropagation(); // 阻止冒泡
                oEvent.preventDefault();  // 阻止默认时间
            }else{
                // IE
                oEvent.cancelBubble = true; // 阻止冒泡
                oEvent.returnValue = false; // 阻止默认时间
            }
            alert("a");

        }
        document.onclick = function(){
            alert("b");
        }
    }
    function getId(id){
        return document.getElementById(id)
    }
    </script>
    </body>
    </html>

  • 相关阅读:
    Java中的变量
    Java是什么
    leetcode 75. 颜色分类
    leetcode 283. 移动零
    剑指 Offer 65. 不用加减乘除做加法
    剑指 Offer 53
    剑指 Offer 58
    剑指 Offer 58
    剑指 Offer 57
    剑指 Offer 57. 和为s的两个数字
  • 原文地址:https://www.cnblogs.com/xiuciedward/p/3146006.html
Copyright © 2011-2022 走看看