按键修饰符:主要用于监听键盘上的按钮被按下时,可触发对应的事件函数
v-on:keyup.修饰符.修饰符】、
.enter
.tab
.delete(针对delete和backspace两个按键)
.esc
.space
.esc
.space
.up
.down
.left
.right
系统修饰符必须按下才触发
.ctrl
.alt
.shift
.meta:mac针对的是command键盘,windows针对的windows键盘(田)
例子代码如下:
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>v-on事件处理指令title>
- head>
- <body>
-
- <div id="app">
-
- <button v-on:click="say">v-on绑定的时间按钮:{{msg}}button>
-
- <button @click="say(123)">简写@绑定的事件按钮:{{msg}}button>
-
- <button @click="doLog('牛逼',$event)">手动传递DOM事件对象button>
-
- <input @blur="doBlur">
-
- <h3>事件修饰符h3>
- <div @click="todo">
- <div @click.stop="doThis">
- 单机事件会继续传递
- div>
- div>
- <br>
-
- <a href="http://www.baidu.com" @click="doStop">默认跳转a>
- <br>
-
- <a href="http://www.baidu.com" @click.prevent="doStop">.prevent修饰符-跳转a>
- <br>
-
- <button @click="doOnly1">点击事件每次加一{{num1}}button>
- <br>
- <button @click.once="doOnly">.once修饰符,点击事件只触发一次{{num}}button>
-
- <h3>按键修饰符h3>
- <input v-on:keyup.enter="keyEnter" placeholder="监听回车(enter)事件">
- <input v-on:keyup.delete="keyDelete" placeholder="监听删除(针对delete和backspace两个按键)">
-
- <input @keyup.alt.enter="altEnter" placeholder="监听Alt + center">
- <div @click.ctrl="clickCtrl">Ctrl+点击事件div>
- <input @keyup.meta.delete="mdt" placeholder="监听田 + delete或backspace两个按键">
-
- <input @keyup.ctrl.shift.x="ctrlShiftx" placeholder="ctrlShiftx">
- div>
-
-
-
-
- <script src="./node_modules/vue/dist/vue.global.js">script>
- <script type="text/javascript">
- const {createApp} = Vue;
- const app=createApp({
- data(){
- return{
- msg:"aaa",
- num:0,
- num1:0
- }
- },
- //定义方法的选项
- methods: {
- say(event){//event 事件对象
- this.msg="5555"; //this 代表app对象
- console.log("say something!",event);
- },//多个必须加逗号
- doLog(name,event){
- console.log(name,event);
- },
- doBlur(event){
- console.log('输入框失去焦点',event.target.value); //获取输入框值
- },
- doThis(event){
- console.log("doThis");
- },
- todo(event){
- console.log("todo");
- },
- doStop(){
- alert("doStop!");
- },
- doOnly1(){
- this.num1++;
- console.log("doOnly1");
- },
- doOnly(){
- this.num++;
- console.log("doOnly");
- },
- keyEnter(){
- console.log("监听enter按键");
- },
- keyDelete(event){
- console.log("监听delete和backspace两个按键按键",event.keyCode);
- },
- altEnter(event){
- console.log("监听alt和Enter两个组合按键",event.keyCode);
- },
- clickCtrl(){
- console.log("监听clickCtrl",event.keyCode);
- },
- mdt(){
- console.log("监听田 + delete或backspace两个按键",event.keyCode);
- },
- ctrlShiftx(){
- console.log("ctrlShiftx",event.keyCode);
- }
- }
- }).mount("#app");
- console.log("app",app);
- script>
-
- body>
- html>