Vue中的event对象介绍

 更新时间:2022年03月14日 09:22:13   作者:.NET开发菜鸟  
这篇文章介绍了Vue中的event对象,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、什么是event对象

event对象:代表的是事件的状态。比如获取当前的元素:e.Target。

二、事件冒泡

什么是事件冒泡呢?百度百科的解释如下:

当事件发生后,这个事件就要开始传播(从里到外或者从外向里)。为什么要传播呢?因为事件源本身(可能)并没有处理事件的能力,即处理事件的函数(方法)并未绑定在该事件源上。例如我们点击一个按钮时,就会产生一个click事件,但这个按钮本身可能不能处理这个事件,事件必须从这个按钮传播出去,从而到达能够处理这个事件的代码中(例如我们给按钮的onclick属性赋一个函数的名字,就是让这个函数去处理该按钮的click事件),或者按钮的父级绑定有事件函数,当该点击事件发生在按钮上,按钮本身并无处理事件函数,则传播到父级去处理。

可能下面的例子会更容易理解一些:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 构建vue实例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(){
                       console.log("我的div3");
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3">
                    我的div3
                </div>
            </div>
        </div>
    </div>
</body>
</html>

效果:

在上面的代码中,3个div分别绑定了3个不同的事件,点击"我的div3"的时候

那么该如何阻止事件冒泡呢?

1、原始JS中的处理方法

代码示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 构建vue实例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       e.stopPropagation();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
            </div>
        </div>
    </div>
</body>
</html>

效果:

2、vue中处理方法

代码示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 构建vue实例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修饰符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>
            </div>
        </div>
    </div>
</body>
</html>

效果:

点击"我的div4"的时候会阻止事件冒泡,但点击"我的div3"的时候不会阻止事件冒泡。

三、事件的默认动作

看下面的代码示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 构建vue实例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   },
                   play4:function(e){
                       console.log("我是超链接");
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修饰符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>

                <a href="http://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="play4($event)">click</a>
            </div>
        </div>
    </div>
</body>
</html>

效果:

点击“click”的时候会发现页面跳转到了百度,不会进入play4事件,如果调试代码想进入play4事件该如何处理呢?

1、使用原生JS处理

代码示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 构建vue实例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   },
                   play4:function(e){
                       console.log("我是超链接");
                       // 取消事件的默认动作
                       e.preventDefault();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修饰符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>

                <a href="http://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="play4($event)">click</a>
            </div>
        </div>
    </div>
</body>
</html>

效果:

这里在点击“click”的时候就不会进入百度首页了。这里没有处理冒泡,所以会触发play2和play1事件。

2、使用vue处理

代码示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 构建vue实例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   },
                   play4:function(e){
                       console.log("我是超链接");
                       // 取消事件的默认动作
                       //e.preventDefault();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修饰符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>

                <a href="http://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="play4($event)">click</a>
                <!--使用vue处理-->
                <a href="http://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click.prevent.stop="play4($event)">click2</a>
            </div>
        </div>
    </div>
</body>
</html>

效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue3中事件处理@click的用法详解

    vue3中事件处理@click的用法详解

    @click指令用于监听元素的点击事件,并在触发时执行相应的处理函数,在Vue3中,事件处理就可以通过@click指令来实现,下面我们就来看看如何在Vue3中处理点击事件吧
    2023-08-08
  • Vxe-Table开发中的各种坑以及避坑指南

    Vxe-Table开发中的各种坑以及避坑指南

    vxe-table是一个全功能的Vue表格,满足绝大部分对Table的一切需求,与任意组件库完美兼容,下面这篇文章主要给大家介绍了关于Vxe-Table开发中各种坑以及避坑的相关资料,需要的朋友可以参考下
    2022-09-09
  • Vue EventBus自定义组件事件传递

    Vue EventBus自定义组件事件传递

    这篇文章主要介绍了Vue EventBus自定义组件事件传递,组件化应用构建是Vue的特点之一,本文主要介绍EventBus进行数据消息传递 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Vue.js路由实现选项卡简单实例

    Vue.js路由实现选项卡简单实例

    这篇文章主要为大家详细介绍了Vue.js路由实现选项卡简单实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Vue3中slot插槽基本使用

    Vue3中slot插槽基本使用

    插槽slot可以说在一个Vue项目里面处处都有它的身影,比如我们使用一些UI组件库的时候,我们通常可以使用插槽来自定义我们的内容,这篇文章主要介绍了Vue3中slot插槽使用方式,需要的朋友可以参考下
    2022-08-08
  • 浅析Vue3中Excel下载模板并导入数据功能的实现

    浅析Vue3中Excel下载模板并导入数据功能的实现

    这篇文章主要为大家详细介绍了Vue3中的Excel数据管理,即下载模板并导入数据功能的实现,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下
    2024-05-05
  • Vue表单验证 trigger:'blur'OR trigger:'change'区别解析

    Vue表单验证 trigger:'blur'OR trigger:'change&ap

    利用 elementUI 实现表单元素校验时,出现下拉框内容选中后校验不消失的异常校验情形,这篇文章主要介绍了Vue表单验证 trigger:‘blur‘ OR trigger:‘change‘ 区别,需要的朋友可以参考下
    2023-09-09
  • vue-treeselect无法点击问题(点击无法出现拉下菜单)

    vue-treeselect无法点击问题(点击无法出现拉下菜单)

    这篇文章主要介绍了vue-treeselect无法点击问题(点击无法出现拉下菜单),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • elementUI table如何给表头添加气泡显示

    elementUI table如何给表头添加气泡显示

    这篇文章主要介绍了elementUI table如何给表头添加气泡显示问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • vue中解决el-date-picker更改样式不生效问题

    vue中解决el-date-picker更改样式不生效问题

    在使用Vue.js进行前端开发的过程中,Element UI 是一个非常流行的UI库,它提供了一套完整的组件来快速搭建美观的用户界面,但是我们经常遇到一个问题使用Element UI提供的el-date-picker组件时,尝试自定义其样式却无法生效,所以本文给大家介绍如何解决这个问题
    2024-10-10

最新评论