vue2.x的事件系统

带参和不带参

如果要传参数,必须写(),如不传参数,爱写不写
不写(),默认参数就是event
写() 显式传参,event名字叫:$event

常用的修饰符

阻止默认事件 .prevent
阻止事件冒泡 .stop
一次性事件 .once
自身事件 .self
按键事件 .enter

代码片段

<!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>Document</title>
    <script src="./vue.js"></script>
    <style>
        .box{
            width: 100px;
            height: 100px;
            background: #333333;
        }
        .outer{
            width: 300px;
            height: 300px;
            background: #333333;
            margin: 20px;
        }
        .inner{
            width: 100px;
            height: 100px;
            background: orange;
        }
        .mask{
            width: 100vw;
            height: 100vh;
            background: rgba(0, 0, 0, 0.5);
            position: fixed;
            left: 0px;
            top: 0px;
        }
        .con{
            width: 200px;
            height: 100px;
            position: absolute;
            left:0px;
            top: 0px;
            right: 0px;
            bottom: 0px;
            margin: auto;
            background: #ffffff;
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- @click="alert1()" 如果要传参数,必须写(),如不传参数,爱写不写 -->
        <button @click="alert1(10)">1</button>
        <button @click="alert2(10,20)">2</button>
        <!-- event  -->
        <!-- 1.不写(),默认参数就是event -->
        <button @click="getEvent">event1</button>

        <!-- 2.写() 显式传参,event名字叫:$event -->
        <button @click="getEvent2($event)">event2</button>
        <button @click="getEvent3(10,$event)">event3</button>

        <!-- 阻止默认事件 .prevent-->
        <div class="box" @contextmenu="contextmenu"></div>
        <div class="box" @contextmenu.prevent="contextmenu2"></div>

        <!-- 阻止事件冒泡 .stop-->
        <div class="outer" @click="outerClick">
            <div class="inner" @click.stop.prevent="innerClick"></div>
        </div>
        <!-- 一次性绑定 .once-->
        <div class="inner" @click.once="innerClick"></div>

        <!--目标元素是自己才出发 .self -->
        <div class="outer" @click.self="outerClick">
            <div class="inner" @click="innerClick"></div>
        </div>

        <button @click="isClose=true;">退出登录</button>
        <div class="mask" v-if="isClose" @click.self="isClose=false;">
            <div class="con">
                <h2>你确定要退出吗?</h2>
                <button>确定</button>
                <button @click="isClose=false;">取消</button>
            </div>
        </div>

        <!-- enter 按键 -->
        <input type="text" @keydown.enter="down()" @keydown.up.prevent="up"
        @keydown.down="down2"
        >
    </div>
    <script>
        new Vue({
            el:"#app",//挂载点
            data:{//属性
                isClose:false
            },
            methods:{//方法
                alert1(n){
                    alert(n)
                },
                alert2(a,b){
                    alert(a+b)
                },
                getEvent(e){
                    console.log(e)
                },
                getEvent2(ev){
                    console.log(ev)
                },
                getEvent3(e,n){
                    console.log(e)
                },
                contextmenu(e){
                    e.preventDefault();
                    console.log("yjl");
                },
                contextmenu2(){
                    console.log("yjl2")
                },
                innerClick(){
                    console.log("inner Click")
                },
                outerClick(){
                    
                    console.log("outer Click")
                },
                down(){
                    console.log("enter")
                },
                up(){
                    console.log("up")
                },
                down2(){
                    console.log("down")
                }
            }
        })
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值