zoukankan      html  css  js  c++  java
  • Vue之事件冒泡

    1. 原生事件冒泡

    <!DOCTYPE html>  
    <html lang="en">  
    <head>  
        <meta charset="UTF-8">  
        <title></title>  
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
        <meta name="apple-mobile-web-app-capable" content="yes">  
        <meta name="apple-mobile-web-app-status-bar-style" content="black">  
        <style>  
      
        </style>  
        <script src="vue.js"></script>  
        <script>  
            window.onload=function(){  
                new Vue({  
                    el:'#box',  
                    data:{  
      
                    },  
                    methods:{  
                        show:function(ev){  
                            alert(1);  
                            //ev.cancelBubble=true;  
                        },  
                        show2:function(){  
                            alert(2);  
                        }  
                    }  
                });  
            };  
        </script>  
    </head>  
    <body>  
        <div id="box">  
            <div @click="show2()">  
                <input type="button" value="按钮" @click="show($event)">  
            </div>  
        </div>  
    </body>  
    </html>

    事件冒泡:

    从内向外扩散,点击input,会扩散到外层的div,一直向上扩散。点击完按钮,会依次执行事件show()和show2()

    结果:

    先弹出1,后弹出2

    使用原生js取消冒泡:ev.cancelBubble=true;

    <!DOCTYPE html>  
    <html lang="en">  
    <head>  
        <meta charset="UTF-8">  
        <title></title>  
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
        <meta name="apple-mobile-web-app-capable" content="yes">  
        <meta name="apple-mobile-web-app-status-bar-style" content="black">  
        <style>  
      
        </style>  
        <script src="vue.js"></script>  
        <script>  
            window.onload=function(){  
                new Vue({  
                    el:'#box',  
                    data:{  
      
                    },  
                    methods:{  
                        show:function(ev){  
                            alert(1);  
                            ev.cancelBubble=true;  
                        },  
                        show2:function(){  
                            alert(2);  
                        }  
                    }  
                });  
            };  
        </script>  
    </head>  
    <body>  
        <div id="box">  
            <div @click="show2()">  
                <input type="button" value="按钮" @click="show($event)">  
            </div>  
        </div>  
    </body>  
    </html>  

    点完按钮后,只弹出1来。

    2.Vue中事件冒泡

    取消事件冒泡:使用 .stop来取消事件冒泡

    <!DOCTYPE html>  
    <html lang="en">  
    <head>  
        <meta charset="UTF-8">  
        <title></title>  
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
        <meta name="apple-mobile-web-app-capable" content="yes">  
        <meta name="apple-mobile-web-app-status-bar-style" content="black">  
        <style>  
      
        </style>  
        <script src="vue.js"></script>  
        <script>  
            window.onload=function(){  
                new Vue({  
                    el:'#box',  
                    data:{  
      
                    },  
                    methods:{  
                        show:function(){  
                            alert(1);  
                        },  
                        show2:function(){  
                            alert(2);  
                        }  
                    }  
                });  
            };  
        </script>  
    </head>  
    <body>  
        <div id="box">  
            <div @click="show2()">  
                <input type="button" value="按钮" @click.stop="show()">  
            </div>  
        </div>  
    </body>  
    </html>  
  • 相关阅读:
    学习:类和对象——继承
    学习:类和对象——运算符重载
    域权限维持:Skeleton Key
    域权限维持:SSP密码记录
    学习:类和对象——友元
    学习:类和对象——对象模型和this指针
    学习:类和对象——静态成员变量和函数
    学习:类和对象——初始化列表和内部类
    学习:类和对象——深拷贝和浅拷贝
    二维数组中的查找
  • 原文地址:https://www.cnblogs.com/chaofei/p/7706781.html
Copyright © 2011-2022 走看看