javascript中的preventDefault与stopPropagation作用介绍

preventDefault方法的起什么作用呢?

我们知道比如<a href="http://www.baidu.com">百度</a>,这是html中最基础的东西,起的作用就是点击百度链接到http://www.baidu.com,这是属于<a>标签的默认行为。

看一段代码大家就明白了:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>JS阻止链接跳转</title>
        <script type="text/javascript">
            function stopDefault( e ) {
                if ( e && e.preventDefault ) {
                    e.preventDefault();
                } else {
                    window.event.returnValue = false;
                }
                return false;
            }
        </script> 
    </head>
    <body>
        <a href="http://www.baidu.com" id="testLink">百度</a> 
        <script type="text/javascript">
            var test = document.getElementById( 'testLink' );
            test.onclick = function( e ) {
                alert( '我的链接地址是:' + this.href + ', 但是我不会跳转。' );
                stopDefault( e );
            }
        </script>
    </body>
</html>

此时点击百度链接,不会打开http://www.baidu.com,而只是弹出一个alert对话框。


preventDefault方法讲解到这里,stopPropagation方法呢?

讲stopPropagation方法之前必需先给大家讲解一下js的事件代理。

事件代理用到了两个在JavaSciprt事件中常被忽略的特性:事件冒泡以及目标元素。当一个元素上的事件被触发的时候,比如说鼠标点击了一个按钮,同样的事件将会在那个元素的所有祖先元素中被触发。这一过程被称为事件冒泡;这个事件从原始元素开始一直冒泡到DOM树的最上层。对任何一个事件来说,其目标元素都是原始元素,在我们的这个例子中也就是按钮。目标元素它在我们的事件对象中以属性的形式出现。使用事件代理的话我们可以把事件处理器添加到一个元素上,等待事件从它的子级元素里冒泡上来,并且可以很方便地判断出这个事件是从哪个元素开始的。

stopPropagation方法又起什么作用?

stopPropagation是可以阻止它的默认行为的发生而发生其他的事情。起到阻止js事件冒泡的作用。

看一段代码。
<!DOCTYPE html>
<html>
    <head>
        <title> 阻止JS事件冒泡传递(cancelBubble 、stopPropagation)</title>
        <meta name="keywords" content="JS,事件冒泡,cancelBubble,stopPropagation" />
        <script>
            function doSomething( obj, evt ) {
                alert( obj.id );
                var e = ( evt ) ? evt : window.event;
                if ( window.event ) {
                    e.cancelBubble = true;// ie下阻止冒泡
                } else {
                    //e.preventDefault();
                    e.stopPropagation();// 其它浏览器下阻止冒泡
                }
            }
        </script>
    </head>
    <body>
        <div id="parent1" οnclick="alert( this.id )" style="width:250px;background-color:yellow">
            This is parent1 div. 
            <div id="child1" οnclick="alert( this.id )" style="width:200px;background-color:orange">
                This is child1. 
            </div>
            This is parent1 div. 
        </div>
        <br />
        <div id="parent2" οnclick="alert( this.id )" style="width:250px;background-color:cyan;">
            This is parent2 div. 
            <div id="child2" οnclick="doSomething( this, event );" style="width:200px;background-color:lightblue;">
                This is child2. Will bubble. 
            </div>
            This is parent2 div. 
        </div>
    </body>
</html>

大家运行一下上面的代码就明白了。


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值