事件对象(event)、事件冒泡、默认行为

1.点击事件的event(事件对象)

    <div id="box">
        <input type="button" value="按钮" @click="show($event)"> 
    </div>
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
            var vm = new Vue({
                el:'#box',
                methods:{
                    show:function(event){
                        console.log(event);
                        alert(event.clientX); //这里边就是原生JS
                    }
                }
            });
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

调用函数的时候传入一个$event参数进去,函数内部可以通过这个event参数,获取鼠标点击的X点和Y点。

2.事件冒泡

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script src="http://unpkg.com/vue/dist/vue.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            var vm = new Vue({
                el:'#box',
                methods:{
                    show:function(){
                        alert(1);
                    },
                    show2:function(){
                        alert(2);
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div id="box">
        <div @click="show2()">
            <input type="button" value="按钮" @click="show()"> 
        </div>
    </div>
</body>
</html>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

这里写图片描述 
点击“按钮”,会依次弹出12。说明先执行了show()方法,然后执行了show2()方法。这就是“事件冒泡”。

阻止冒泡

利用我们上面讲过的event对象。 
在调用函数时传入:

    <div id="box">
        <div @click="show2()">
            <input type="button" value="按钮" @click="show($event)"> 
        </div>
    </div>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

然后在show()方法里可以组织冒泡:

                    show:function(event){
                        alert(1);
                        event.cancelBubble = true;
                    },
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

这样show2()就不会执行了。

利用vue的方法阻止冒泡

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script src="http://unpkg.com/vue/dist/vue.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            var vm = new Vue({
                el:'#box',
                methods:{
                    show:function(event){
                        alert(1);
                        //event.cancelBubble = true;
                    },
                    show2:function(){
                        alert(2);
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div id="box">
        <div @click="show2()">
            <input type="button" value="按钮" @click.stop="show()"> 
        </div>
    </div>
</body>
</html>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

注意此时我们没有event对象,而是在给HTML元素绑定click事件的时候,改为@click.stop="show()"

3.默认行为

contextmenu右键菜单

    <div id="box">
        <input type="button" value="按钮" @contextmenu="show()"> 
    </div>
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

点击右键也会执行show()方法,但是执行完之后会打开右键菜单的默认行为。 
这里写图片描述

如何阻止这种默认行为?

@contextmenu.prevent="show()"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值