# prevent & stop
- prevent:阻止默认事件(常用);
- stop:阻止事件冒泡(常用);
| <!DOCTYPE html> |
| <html> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <title>事件修饰符</title> |
| |
| <script type="text/javascript" src="./vue.js"></script> |
| <style> |
| * { |
| margin-top: 20px; |
| } |
| |
| .demo1 { |
| height: 50px; |
| background-color: skyblue; |
| } |
| |
| a { |
| color: #000; |
| list-style: none; |
| text-decoration: none; |
| } |
| </style> |
| </head> |
| |
| <body> |
| <div id="root"> |
| <h2>~</h2> |
| |
| <a href="http://nekoaimer.com" @click.prevent="showInfo">nekoaimer个人博客</a> |
| |
| |
| <div class="demo" @click="showInfo"> |
| <button @click.stop="showInfo">点我提示信息~</button> |
| |
| |
| <a href="http://nekoaimer.com" @click.prevent.stop="showInfo">nekoaimer个 |
| 人博客</a> |
| </div> |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| Vue.config.productionTip = false |
| |
| new Vue({ |
| el: '#root', |
| data: { |
| message: 'Hello World' |
| }, |
| methods: { |
| showInfo(e) { |
| console.log('点我提示信息~') |
| |
| } |
| } |
| }) |
| </script> |
| |
| </html> |
# once & capture
- once:事件只触发一次(常用);
- capture:使用事件的捕获模式;
| <!DOCTYPE html> |
| <html> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <title>事件修饰符</title> |
| |
| <script type="text/javascript" src="./vue.js"></script> |
| <style> |
| * { |
| margin-top: 20px; |
| } |
| |
| .box1 { |
| padding: 5px; |
| background-color: skyblue; |
| } |
| |
| .box2 { |
| padding: 5px; |
| background-color: orange; |
| } |
| </style> |
| </head> |
| |
| <body> |
| <div id="app"> |
| |
| <button @click.once="showInfo">事件只触发一次</button> |
| |
| |
| <div class="box1" @click.capture="showMsg(1)"> |
| div1 |
| <div class="box2" @click="showMsg(2)"> |
| div2 |
| </div> |
| </div> |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| Vue.config.productionTip = false |
| |
| new Vue({ |
| el: '#app', |
| data: { |
| message: 'Hello World' |
| }, |
| methods: { |
| showInfo(e) { |
| console.log('点我提示信息~') |
| |
| }, |
| showMsg(msg) { |
| console.log(msg) |
| }, |
| } |
| }) |
| </script> |
| |
| </html> |
# self & passive
- self:只有 event.target 是当前操作的元素时才触发事件;
- passive:事件的默认行为立即执行,无需等待事件回调执行完毕;
| <!DOCTYPE html> |
| <html> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <title>事件修饰符</title> |
| |
| <script type="text/javascript" src="../js/vue.js"></script> |
| <style> |
| * { |
| margin-top: 20px; |
| } |
| |
| .demo { |
| height: 50px; |
| background-color: skyblue; |
| } |
| |
| .list { |
| width: 200px; |
| height: 200px; |
| background-color: peru; |
| overflow: auto; |
| } |
| |
| li { |
| height: 100px; |
| } |
| </style> |
| </head> |
| |
| <body> |
| <div id="app"> |
| |
| <div class="demo" @click.self="showInfo"> |
| <button @click="showInfo">点我提示信息</button> |
| </div> |
| |
| |
| |
| <ul @scroll.passive="demo" class="list"> |
| <li>1</li> |
| <li>2</li> |
| <li>3</li> |
| <li>4</li> |
| </ul> |
| |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| Vue.config.productionTip = false |
| |
| new Vue({ |
| el: '#app', |
| data: { |
| message: 'Hello World' |
| }, |
| methods: { |
| showInfo(e) { |
| alert('同学你好!') |
| |
| }, |
| demo() { |
| for (let i = 0; i < 2333; i++) { |
| console.log('#') |
| } |
| |
| } |
| } |
| }) |
| </script> |
| |
| </html> |
# 键盘事件
# 常用的按键别名
- 回车 => enter
- 删除 => delete (捕获 “删除” 和 “退格” 键)
- 退出 => esc
- 空格 => space
- 换行 => tab (特殊,必须配合 keydown 去使用)
| <!DOCTYPE html> |
| <html> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <title>键盘事件</title> |
| |
| <script type="text/javascript" src="./vue.js"></script> |
| </head> |
| |
| <body> |
| <div id="root"> |
| <input type="text" placeholder="enter" @keydown.enter="showInfo"> |
| <br> |
| <input type="text" placeholder="delete & back" @keydown.delete="showInfo"> |
| <br> |
| <input type="text" placeholder="esc" @keydown.esc="showInfo"> |
| <br> |
| <input type="text" placeholder="space" @keydown.space="showInfo"> |
| <br> |
| <input type="text" placeholder="tab" @keydown.tab="showInfo"> |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| Vue.config.productionTip = false |
| |
| new Vue({ |
| el: '#root', |
| methods: { |
| showInfo(e) { |
| console.log(e.key, e.keyCode) |
| console.log(e.target.value) |
| } |
| }, |
| }) |
| </script> |
| |
| </html> |
# up & down & left & right
- 上 => up
- 下 => down
- 左 => left
- 右 => right
| <!DOCTYPE html> |
| <html> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <title>键盘事件</title> |
| |
| <script type="text/javascript" src="./vue.js"></script> |
| </head> |
| |
| <body> |
| <div id="root"> |
| <input type="text" placeholder="up" @keyup.up="showInfo"> |
| <br> |
| <input type="text" placeholder="down" @keyup.down="showInfo"> |
| <br> |
| <input type="text" placeholder="left" @keyup.left="showInfo"> |
| <br> |
| <input type="text" placeholder="right" @keyup.right="showInfo"> |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| Vue.config.productionTip = false |
| |
| new Vue({ |
| el: '#root', |
| methods: { |
| showInfo(e) { |
| console.log(e.key, e.keyCode) |
| console.log(e.target.value) |
| } |
| }, |
| }) |
| </script> |
| |
| </html> |
- Vue 未提供别名的按键,可以使用按键原始的 key 值去绑定,但注意要转为 kebab-case(短横线命名)
| <input type="text" placeholder="caps-lock" @keydown.caps-lock="showInfo"> |
# 系统修饰键
| <!DOCTYPE html> |
| <html> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <title>键盘事件</title> |
| |
| <script type="text/javascript" src="./vue.js"></script> |
| </head> |
| |
| <body> |
| <div id="root"> |
| <input type="text" placeholder="ctrl" @keydown.ctrl="showInfo"><br> |
| <input type="text" placeholder="alt" @keydown.alt="showInfo"><br> |
| <input type="text" placeholder="cshift" @keydown.shift="showInfo"><br> |
| <input type="text" placeholder="meta" @keydown.meta="showInfo"> |
| |
| <input type="text" placeholder="按下回车提示输入" @keydown.ctrl.y="showInfo"> |
| |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| Vue.config.productionTip = false |
| |
| new Vue({ |
| el: '#root', |
| methods: { |
| showInfo(e) { |
| console.log(e.key, e.keyCode) |
| console.log(e.target.value) |
| } |
| }, |
| }) |
| </script> |
| |
| </html> |
# 自定义别名
- 也可以使用 keyCode 去指定具体的按键(不推荐)
- Vue.config.keyCodes. 自定义键名 = 键码,可以去定制按键别名
| <!DOCTYPE html> |
| <html> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <title>键盘事件</title> |
| |
| <script type="text/javascript" src="./vue.js"></script> |
| </head> |
| |
| <body> |
| <div id="root"> |
| <input type="text" placeholder="按下回车提示输入" @keydown.huiche="showInfo"> |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| Vue.config.productionTip = false |
| Vue.config.keyCodes.huiche = 13 |
| |
| new Vue({ |
| el: '#root', |
| methods: { |
| showInfo(e) { |
| console.log(e.key, e.keyCode) |
| console.log(e.target.value) |
| } |
| }, |
| }) |
| </script> |
| |
| </html> |