zoukankan      html  css  js  c++  java
  • event.preventDefault() --- event.stopPropagation()

    1、event.preventDefault()   阻止默认行为 

    <a href="http://www.baidu.com">链接</a>
    // 阻止默认行为  e.preventDefault();
                $("a").click(function(){
                    event.preventDefault();
                })
    
                //阻止浏览器的默认行为 
                function stopDefault( e ) { 
                    //阻止默认浏览器动作(W3C) 
                    if ( e && e.preventDefault ) 
                        e.preventDefault(); 
                    //IE中阻止函数器默认动作的方式 
                    else 
                        window.event.returnValue = false; 
                    return false; 
                }

    2、event.stopPropagation()  停止冒泡

    <div class="demo" onclick="alert('div')">
            <ul onclick="alert('ul')">
                <li onclick="alert('li')">asd</li>
            </ul>
        </div>
    // 防止冒泡  e.stopPropagation(e);
                $(".demo li").click(function(e){
                    window.event ? window.event.cancelBubble = true : e.stopPropagation();
                })
    
    
                function stopBubble(e) {
    
                    //如果提供了事件对象,则这是一个非IE浏览器 
                    if ( e && e.stopPropagation ) 
                        //因此它支持W3C的stopPropagation()方法 
                        e.stopPropagation(); 
                    else 
                        //否则,我们需要使用IE的方式来取消事件冒泡 
                        window.event.cancelBubble = true; 
                    }
                }
  • 相关阅读:
    java 常用第3方工具
    反射与内省
    UDP通讯协议实例
    多线程及线程并发库Executors
    Deque 双端队列 Stack 堆栈
    Collections工具类
    Map 接口
    PHP控制电脑重启 关机
    ThinkPHP框架研究之一 基本函数 M和D的区别
    textarea输入输出的处理
  • 原文地址:https://www.cnblogs.com/do-it/p/4479162.html
Copyright © 2011-2022 走看看