vue——18-事件修饰符

<div id="enjoy">
    <!--
    使用.stop 阻止冒泡(阻止上级所有层的事件)
    仅son
    -->
    <div class="father" @click="father">
        <div class="son" @click.stop="son"></div>
    </div>

    <!--
    使用.prevent 阻止默认事件
    不会跳转页面
    -->
    <a href="https://www.baidu.com" @click.prevent="preventBehavior">百度一下</a>

    <!--
    使用.capture 添加事件侦听器时使用事件捕获模式(一般默认冒泡事件由里向外)
    先father后son
    -->
    <div class="father" @click.capture="father">
        <div class="son" @click="son"></div>
    </div>

    <!--
    使用.self 实现只有点击当前元素时,才会触发事件函数(仅阻止当前事件冒泡)
    只有点father时弹窗,点son不弹
    -->
    <div class="father" @click.self="father">
        <div class="son" @click="son"></div>
    </div>

    <!--
    使用.once 只触发一次事件处理函数(此例中连带.prevent也失效)
    第一次点击仅弹窗,之后正常跳转不弹窗
    -->
    <a href="https://www.baidu.com" @click.prevent.once="preventBehavior">百度一下</a>
</div>

        {
            new Vue({
                el:'#enjoy',
                data:{},
                methods:{
                    father(){
                        alert('father');
                    },
                    son(){
                        alert('son');
                    },
                    preventBehavior(){
                        alert('hello');
                    }
                }
            })
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值