Vue模板语法——键盘事件修饰符

一、键盘修饰符

在JavaScript事件中除了前面所说的事件,还有键盘事件,也经常需要监测常见的键值。在Vue中允许v-on在监听键盘事件时添加关键修饰符。记住所有的keyCode比较困难,所以Vue为最常用的键盘事件提供了别名:
  • enter:回车键
  • tab:制表键
  • delete:含delete和backspace键
  • esc:返回键
  • space: 空格键
  • up:向上键
  • down:向下键
  • left:向左键
  • right:向右键

注意: 键盘修饰符须添加的标签中绑定keyup事件名,之后加上键盘修饰符

例如: @keyup.修饰符="方法" => @keyup.enter="handle"

    v-on:keyup.修饰符="方法" => `v-on:keyup.enter="handle"`

二、enter修饰符

  1. .enter是点击enter键触发绑定方法

  2. 例子

        <div id="app">
            <form action="" method="post">
                <div>
                    <input type="text" v-model="uname"/>
                </div>
                <div>
                    <input type="password" v-model="pwd" @keyup.enter="handleSubmit"/>
                </div>
                <div>
                    <input type="button" @click="handleSubmit" value="提交">
                </div>
            </form>
        </div>
        <script src="../js/vue.js"></script>
        <script>
            var vm = new Vue({
                el: "#app",
                data: {
                    uname:'',
                    pwd:''
                },
                methods: {
                    handleSubmit:function(){
                        console.log(this.uname,this.pwd);
                    }
                }
            })
        </script>
    

    三、delete修饰符

    1. .delete是点击delete键触发绑定方法
    2. 例子
        <div id="app">
            <form action="" method="post">
                <div>
                    <input type="text" v-model="uname" v-on:keyup.delete="clearContent"/>
                </div>
                <div>
                    <input type="password" v-model="pwd" @keyup.enter="handleSubmit"/>
                </div>
                <div>
                    <input type="button" @click="handleSubmit" value="提交">
                </div>
            </form>
        </div>
        <script src="../js/vue.js"></script>
        <script>
            var vm = new Vue({
                el: "#app",
                data: {
                    uname:'',
                    pwd:''
                },
                methods: {
                    handleSubmit:function(){
                        console.log(this.uname,this.pwd);
                    },
                    clearContent:function(){
                        this.uname = '';
                    }
                }
            })
        </script>
    
posted @ 2020-03-17 16:04  无他但手熟尔  阅读(406)  评论(0编辑  收藏  举报