Vue中的常用的事件修饰符


一. 事件修饰符

在vue中了解一些事件修饰符,可以跟高效的辅助我们开发,一下是一些常用的事件修饰符
.stop 阻止事件冒泡
.prevent 阻止默认事件发送
.once 让方法只能执行一次
.capture 事件捕获
.self 只有点击自己才会触发
一些常用的键盘事件
.up 向上
.left 向左
.right 向右
.down 向下
.enter enter键
.13 键盘编码

1.1 .stop

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>

   .parent {
      width: 300px;
      height: 300px;
      border: 3px solid #000;
   }
   .child {
        width: 200px;
        height: 200px;
        border: 2px solid red;
   }

    </style>
</head>
<body>
    <div id="app">
<!-- 
   常用的事件修饰符 
   .once 使用一次
   .capture 事件捕获
   .self  只有点击自己触发
   .stop 阻止冒泡事件
   .prevent 阻止默认事件发生
   键盘事件
   .up 向上
   .left 向左
   .right 向右
   .down 向下
   .13   键盘事件
 -->
    <div class="parent" @click="parent">
        parent
        <div class="child" @click.stop="child">child</div>
    </div>             


    </div>
</body>
<script>
   new Vue({
     el:"#app",
     data:{

     },
     methods:{
         child(){
             console.log('child');
         },
         parent(){
             console.log('parent');
         }
     }

   })



</script>
</html>

在这里插入图片描述

1.2 .self

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>

   .parent {
      width: 300px;
      height: 300px;
      border: 3px solid #000;
   }
   .child {
        width: 200px;
        height: 200px;
        border: 2px solid red;
   }

    </style>
</head>
<body>
    <div id="app">
<!-- 
   常用的事件修饰符 
   .once 使用一次
   .capture 事件捕获
   .self  只有点击自己触发
   .stop 阻止冒泡事件
   .prevent 阻止默认事件发生
   键盘事件
   .up 向上
   .left 向左
   .right 向右
   .down 向下
   .13   键盘事件
 -->
    <div class="parent" @click.self="parent">
        parent
        <div class="child" @click="child">child</div>
    </div>             


    </div>
</body>
<script>
   new Vue({
     el:"#app",
     data:{

     },
     methods:{
         child(){
             console.log('child');
         },
         parent(){
             console.log('parent');
         }
     }

   })



</script>
</html>

在这里插入图片描述

1.3 .capture

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>

   .parent {
      width: 300px;
      height: 300px;
      border: 3px solid #000;
   }
   .child {
        width: 200px;
        height: 200px;
        border: 2px solid red;
   }

    </style>
</head>
<body>
    <div id="app">
<!-- 
   常用的事件修饰符 
   .once 使用一次
   .capture 事件捕获
   .self  只有点击自己触发
   .stop 阻止冒泡事件
   .prevent 阻止默认事件发生
   键盘事件
   .up 向上
   .left 向左
   .right 向右
   .down 向下
   .13   键盘事件
 -->
    <div class="parent" @click.capture="parent">
        parent
        <div class="child" @click="child">child</div>
    </div>             


    </div>
</body>
<script>
   new Vue({
     el:"#app",
     data:{

     },
     methods:{
         child(){
             console.log('child');
         },
         parent(){
             console.log('parent');
         }
     }

   })



</script>
</html>

在这里插入图片描述

1.4 .prevent

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>

   .parent {
      width: 300px;
      height: 300px;
      border: 3px solid #000;
   }
   .child {
        width: 200px;
        height: 200px;
        border: 2px solid red;
   }

    </style>
</head>
<body>
    <div id="app">
<!-- 
   常用的事件修饰符 
   .once 使用一次
   .capture 事件捕获
   .self  只有点击自己触发
   .stop 阻止冒泡事件
   .prevent 阻止默认事件发生
   键盘事件
   .up 向上
   .left 向左
   .right 向右
   .down 向下
   .13   键盘事件
 -->
        num:{{num}} <br>
        <button @click.once="add">点击增加</button>

    </div>             


    </div>
</body>
<script>
   new Vue({
     el:"#app",
     data:{
         num:1
     },
     methods:{
         child(){
             console.log('child');
         },
         parent(){
             console.log('parent');
         },
         add(){
            this.num++
         }
     }

   })



</script>
</html>

在这里插入图片描述

1.5 prevent

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>

   .parent {
      width: 300px;
      height: 300px;
      border: 3px solid #000;
   }
   .child {
        width: 200px;
        height: 200px;
        border: 2px solid red;
   }

    </style>
</head>
<body>
    <div id="app">
<!-- 
   常用的事件修饰符 
   .once 使用一次
   .capture 事件捕获
   .self  只有点击自己触发
   .stop 阻止冒泡事件
   .prevent 阻止默认事件发生
   键盘事件
   .up 向上
   .left 向左
   .right 向右
   .down 向下
   .13   键盘事件
 -->
      <a href="" @click.prevent="fun">跳转</a>

    </div>             


    </div>
</body>
<script>
   new Vue({
     el:"#app",
     data:{
         num:1
     },
     methods:{
         fun(){
            console.log('1234');
         }
     }

   })



</script>
</html>

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值