惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Jina AI
Jina AI
罗磊的独立博客
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
月光博客
月光博客
博客园_首页
Vercel News
Vercel News
P
Proofpoint News Feed
GbyAI
GbyAI
Y
Y Combinator Blog

博客园 - linFen

Ubuntu下使用ProcDump ubuntu 20.04 搭建 ProcDump Anaconda 常用命令 记录codesmith 断点调试弹不了vs Vue模板语法——事件修饰符 Vue模板语法——v-on 事件绑定 Vue模板语法——v-model 双向数据绑定 Vue模板语法——v-once 数据响应式 Vue模板语法——数据绑定指令 Vue模板语法——v-cloak vue响应性原理 ES6其他 css其他 JS其他 前端VUE 前端 vue 中slot Vue 之 Mixins (混入) Vue 响应性
Vue模板语法——键盘事件修饰符
linFen · 2024-01-28 · via 博客园 - linFen

一、键盘修饰符

在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>