温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue阻止事件

发布时间:2020-07-03 08:37:16 来源:网络 阅读:1096 作者:梦想代码 栏目:web开发

1.事情对象

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){
                        console.log(event);   //event   这个就是事件对象了                    }
                }
            });
        }    </script></head><body>
   <div id="box">
        <input type="button" value="按钮" @click="show($event)"> 
    </div></body></html>

vue阻止事件

通过show($event)把事件对象传到方法里

 

2.事件冒泡

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(){
                        alert(1);
                    },
                    show1:function(){
                        alert(2);
                    }
                }
            });
        }    </script></head><body>
    <div id="box">
        <div @click="show1()">
            <input type="button" value="按钮" @click="show()"> 
        </div>
    </div></body></html>

vue阻止事件

点击按钮的话他会,执行show ,show1方法,依次弹出1,2。

 

怎么来阻止

<1> 利用我们上面讲过的event对象:  event.cancelBubble = true;   //这种就阻止了

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;
                    },
                    show1:function(){
                        alert(2);
                    }
                }
            });
        }    </script></head><body>
   <div id="box">
        <div @click="show1()">
            <input type="button" value="按钮" @click="show($event)"> 
        </div>
    </div></body></html>

vue阻止事件

 

<2>利用vue的方法阻止冒泡:给HTML元素绑定click事件的时候,改为@click.stop="show()"

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;                    },
                    show1:function(){
                        alert(2);
                    }
                }
            });
        }    </script></head><body>
    <div id="box">
        <div @click="show1()">
            <input type="button" value="按钮" @click.stop="show()"> 
        </div>
    </div></body></html>

vue阻止事件

 

3.默认行为

比如contextmenu右键菜单

vue阻止事件

<!DOCTYPE html><html><head>
    <title></title>
    <meta charset="utf-8">
    <!-- // <script src="vue.js"></script> -->
    <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);
                    },
                    show1:function(){
                        alert(2);
                    }
                }
            });
        }    </script></head><body>
    <div id="box">
        <input type="button" value="按钮" @contextmenu="show()"> 
        <input type="button" value="按钮1" @contextmenu.prevent="show1()"> 

        <p>//按钮右击点下去会依次出现 弹窗 1, 还有右击的默认菜单</p>
        <p>//按钮1右击只出现 弹窗 2</p>
    </div></body></html>

vue阻止事件

@click.stop.prevent,相当于同时执行:
event.stopPropagation();
event.preventDefault(); 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI