zoukankan      html  css  js  c++  java
  • 取消默认事件及冒泡

    阻止冒泡事件

    var el = window.document.getElementById("a");
    el.onclick = function (e) {
        //如果提供了事件对象,则这是一个非IE浏览器
        if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
        }
        else {
            //否则,我们需要使用IE的方式来取消事件冒泡 
            window.event.cancelBubble = true;
            return false;
        }
    }

    阻止默认事件

    var el = window.document.getElementById("a");
    el.onclick = function (e) {
        //如果提供了事件对象,则这是一个非IE浏览器
        if (e && e.preventDefault) {
            //阻止默认浏览器动作(W3C) 
            e.preventDefault();
        }
        else {
            //IE中阻止函数器默认动作的方式 
            window.event.returnValue = false;
            return false;
        }
    }

    jQeury阻止默认和冒泡事件

    阻止冒泡事件
       $("a").click(function (e) {
                e.stopPropagation();
            });
    阻止默认事件
     $("a").click(function (e) {
                e.preventDefault();
            });
    阻止默认和冒泡事件
    $("a").click(function (e) {
                return false;
            });
  • 相关阅读:
    1143 Lowest Common Ancestor (30)
    PAT 1135 Is It A Red-Black Tree
    PAT 1119 Pre- and Post-order Traversals
    1102 Invert a Binary Tree(25 分)
    PAT总结
    c++ getline的用法
    PAT 1049 Counting Ones (30)
    PAT 1022 Digital Library (30)
    java jar包
    NIO的理解
  • 原文地址:https://www.cnblogs.com/liangfc/p/9463624.html
Copyright © 2011-2022 走看看