Vue-9、Vue事件修饰符

1、prevent 阻止默认事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
    <div id="root">
        <!--prevent 阻止默认事件(常用)-->
        <a :href="url" @click.prevent="showinfo">点我提示信息</a>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip=false;
        new Vue({
   
            el:"#root",
            data:{
   
                url:'http://www.atguigu.com'
            },
            methods:{
   
                showinfo(){
   
                    alert("prevent,阻止默认事件")
                }
            }
        })
    </script>
</body>
</html>

2、stop 阻止事件冒泡

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
   </style>
</head>
<body>
    <div id="root">
        <!--stop 阻止事件冒泡(常用)-->
        <div class="demo1" @click="showinfo2">
            <button @click.top="showinfo">点我提示信息</button>
        </div>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip=false;
        new Vue({
   
            el:"#root",
            data:{
   
                url:'http://www.atguigu.com'
            },
            methods:{
   
                showinfo(){
   
                    alert("你好")
                },
                showinfo2(){
   
                    alert("事件冒泡")
                },
            }
        })
    </script>
</body>
</html>

另外一种写法 e.stopPropagation();

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
   </style>
</head>
<body>
    <div id="root">
        <!--stop 阻止事件冒泡(常用)-->
        <div class="demo1" @click="showinfo2">
            <button @click="showinfo">点我提示信息</button>
        </div>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip=false;
        new Vue({
   
            el:"#root",
            data:{
   
                url:'http://www.atguigu.com'
            },
            methods:{
   
                showinfo(e){
   
                    e.stopPropagation();
                    alert("你好")
                },
                showinfo2(){
   
                    alert("事件冒泡")
                },
            }
        })
    </script>
</body>
</html>

3、once 事件只触发一次(常用)

  <button @click.once="showinfo">点我提示信息</button>

4、capture: 使用事件的捕获形式

未使用前

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
       .box1{
   
           padding: 5px;
           background-color: skyblue;
       }
       .box2{
   
           padding: 5px;
           background-color: orange;
       }
   </style>
</head>
<body>
    <div id="root">
        <!-- capture: 使用事件的捕获形式-->
        <div class="box1" @click="showmessage(1)">
            div1
        <div class="box2" @click="showmessage(2)">
            div2
        </div>
        </div>
     </div>
     <script type="text/javascript">
         Vue.config.productionTip=false;
         new Vue({
   
             el:"#root",
             data:{
   
                 url:'http://www.atguigu.com'
             },
             methods:{
   
                 showmessage(msg){
   
                     console.log(msg)
                 },

             }
         })
     </script>
 </body>
 </html>

在这里插入图片描述
使用后

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
       .box1{
   
           padding: 5px;
           background-color: skyblue;
       }
       .box2{
   
           padding: 5px;
           background-color: orange;
       }
   </style>
</head>
<body>
    <div id="root">
        <!-- capture: 使用事件的捕获形式-->
        <div class="box1" @click.capture="showmessage(1)">
            div1
        <div class="box2" @click="showmessage(2)">
            div2
        </div>
        </div>
     </div>
     <script type="text/javascript">
         Vue.config.productionTip=false;
         new Vue({
   
             el:"#root",
             data:{
   
                 url:'http://www.atguigu.com'
             },
             methods:{
   
                 showmessage(msg){
   
                     console.log(msg)
                 },

             }
         })
     </script>
 </body>
 </html>

在这里插入图片描述
5、self: 只有event.target是当前操作的元素时才触发事件
使用前

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
       .box1{
   
           padding: 5px;
           background-color: skyblue;
       }
       .box2{
   
           padding: 5px;
           background-color: orange;
       }
   </style>
</head>
<body>
    <div id="root">
        <!-- self: 只有event.target是当前操作的元素时才触发事件-->
        <div class="demo1" @click="showInfo">
            <button @click="showInfo">点我提示信息</button>
        </div>
     </div>
     <script type="text/javascript">
         Vue.config.productionTip=false;
         new Vue({
   
             el:"#root",
             methods:{
   
                 showInfo(e){
   
                     //alert("你好啊")
                     console.log(e.target);
                 },
             }
         })
     </script>
 </body>
 </html>

效果
在这里插入图片描述
使用后

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
       .box1{
   
           padding: 5px;
           background-color: skyblue;
       }
       .box2{
   
           padding: 5px;
           background-color: orange;
       }
   </style>
</head>
<body>
    <div id="root">
        <!-- self: 只有event.target是当前操作的元素时才触发事件-->
        <div class="demo1" @click.self="showInfo">
            <button @click="showInfo">点我提示信息</button>
        </div>
     </div>
     <script type="text/javascript">
         Vue.config.productionTip=false;
         new Vue({
   
             el:"#root",
             methods:{
   
                 showInfo(e){
   
                     //alert("你好啊")
                     console.log(e.target);
                 },
             }
         })
     </script>
 </body>
 </html>

效果
在这里插入图片描述
6、鼠标滚轮滚动 @wheel 和 滚轮条滚动 @scroll

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
       .box1{
   
           padding: 5px;
           background-color: skyblue;
       }
       .box2{
   
           padding: 5px;
           background-color: orange;
       }

       .list{
   
           width:200px;
           height: 200px;
           background-color: peru;
           overflow: auto;
       }
       li{
   
           height: 100px;
       }

   </style>
</head>
<body>
    <div id="root">
        <!--鼠标滚轮滚动  @wheel-->
        <ul @wheel="demo" class="list">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <!--滚轮条滚动  @scroll-->
        <ul @scroll="demo" class="list">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>



     </div>
     <script type="text/javascript">
         Vue.config.productionTip=false;
         new Vue({
   
             el:"#root",
             methods:{
   
                 demo(){
   
                     console.log('@');
                 }
             }
         })
     </script>
 </body>
 </html>

passive 事件的默认行为立即执行,无需等待事件回调执行完毕

passive使用前

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件修饰符</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <style>
       *{
   
           margin-top: 20px;
       }
       .demo1{
   
           height: 50px;
           background-color: skyblue;
       }
       .box1{
   
           padding: 5px;
           background-color: skyblue;
       }
       .box2{
   
           padding: 5px;
           background-color: orange;
       }

       .list{
   
           width:200px;
           height: 200px;
           background-color: peru;
           overflow: auto;
       }
       li{
   
           height: 100px;
       }

   </style>
</head>
<body>
    <div id="root">

        <!--鼠标滚轮滚动  @wheel-->
        <ul @wheel="demo" class="list">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
     </div>
     <script type="text/javascript">
         Vue.config.productionTip=false;
         new Vue({
   
             el:"#root",
             methods:{
   
                 demo(){
   
                     for (let i = 0; i < 100000; i++) {
   
                         console.log('#')
                     }
                     console.log('累坏了')
                 }
             }
         })
     </script>
 </body>
 </html>

在这里插入图片描述
使用后
在这里插入图片描述
在这里插入图片描述

注意:prevent 和stop可以连着一起使用、

在这里插入图片描述

相关推荐

  1. vue里面事件修饰符.prevent使用案例

    2024-01-11 02:58:02       13 阅读
  2. vue事件修饰符和常用的按键别名

    2024-01-11 02:58:02       26 阅读
  3. Vue 修饰符有哪些

    2024-01-11 02:58:02       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-11 02:58:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-11 02:58:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-11 02:58:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-11 02:58:02       20 阅读

热门阅读

  1. 手搓没有softmax 的gpt

    2024-01-11 02:58:02       35 阅读
  2. 平衡合规与发展天平, 激发数据要素价值

    2024-01-11 02:58:02       33 阅读
  3. Mr_HJ / form-generator项目文档学习与记录(续1)

    2024-01-11 02:58:02       37 阅读
  4. 【Leetcode】19. 删除链表的倒数第 N 个结点

    2024-01-11 02:58:02       38 阅读
  5. vue 的动态饼图

    2024-01-11 02:58:02       40 阅读
  6. RocketMQ双主双从搭建

    2024-01-11 02:58:02       34 阅读