事件处理
- 使用
v-on:事件名=“函数名”
或@事件名=“函数名”
绑定事件; - 事件的回调需要配置在
methods
对象中,最终会在vm
上; methods
中配置的函数,不要用箭头函数,否则this就不是vm了;被vue管理的函数不要使用箭头函数,以确保this都是vm(vue对象)- methods中配置的函数,都是被
Vue所管理的函数
,this的指向是vm或组件实例对象;(methods中的函数是不会进行数据代理的,只有data中的数据vue才进行数据代理) - 函数名后面可以加也可以不加(),如果想要传参数可以添加(),里面填写参数。但是传参是需要注意一个问题,因为v-on是监听事件所以会有一个默认参数event,所以如果需要传递参数时要写成:
v-on:事件名="函数名($event,参数)"
。同时对应的函数要定义如下参数进行接收:函数名(event,参数)
鼠标事件
事件名是click
eg:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>欢迎,{{name}}</h1>
<button v-on:click="showInfo1">点我提示信息</button>
<button @click="showInfo2($event,12)">点我提示信息</button>
</div>
<script type='text/javascript'>
Vue.config.productionTip = false;
new Vue({
el:'#root',
data: {
name:"yang"
},
methods:{
showInfo1(event){
console.log(event.target)//输出的是被点击的对象
// 被vue管理的函数不要使用箭头函数
alert("hello")
},
showInfo2(event,number){
console.log(number)
alert("hello2")
}
}
})
</script>
</body>
</html>
注: @click="demo”
和@click="demo($event)”
效果一致,但后者可以传参;
滚动事件
滚动条滚动
@scroll="函数名"
v-on:scroll="函数名"
滚动轮滚动
@wheel="函数名"
v-on:wheel="函数名"
键盘事件
键盘按下:
@keydown="函数名"
v-on:keydown="函数名"
键盘抬起:
@keyup="函数名"
v-on:keyup="函数名"
eg:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h2>欢迎,{{name}}</h2>
<!-- .enter Vue的按键别名,在这里表示当按下enter键才执行后面的函数 -->
<input type="text" placeholder="按下回车提示输入" @keyup="showInfo" />
</div>
<script type="text/javascript">
Vue.config.productionTip = false;
Vue.config.keyCodes.huiche=13
new Vue({
el: "#root",
data: {
name: "yang",
},
methods: {
showInfo(event) {
console.log(event.target.value)
},
},
});
</script>
</body>
</html>
Vue中常用的按键别名
- Vue的按键别名
回车:enter
删除:delete
(捕获"删除”和“退格”键)
退出:esc
空格:space
换行:tab
(特殊,必须配合keydown去使用,因为tab点击完之后就失去了焦点,所以不能和keyup结合,需要和keydown结合使用)
上 :up
下 :down
左:left
右:right
eg:
<input type="text" placeholder="按下回车提示输入" @keyup.enter="showInfo" />
表示按下enter时才执行showInfo
函数。
- Vue未提供别名的按键,可以使用按键原始的key值去绑定,但注意如果是由两个或多个单词组成的键名,要转为
kebab-case
(短横线命名)
获取键盘key值(即键盘名字)的方法:event.key
eg:
<body>
<div id="root">
<h2>欢迎,{{name}}</h2>
<!-- .enter Vue的按键别名,在这里表示当按下enter键才执行后面的函数 -->
<input type="text" placeholder="按下回车提示输入" @keyup="showInfo" />
</div>
<script type="text/javascript">
Vue.config.productionTip = false;
new Vue({
el: "#root",
data: {
name: "yang",
},
methods: {
showInfo(event) {
// event.key键的名字,event.keyCode键的编码
console.log(event.key,event.keyCode)
},
},
});
</script>
</body>
eg:转换大小写
的键
<input type="text" placeholder="按下回车提示输入" @keyup.caps-lock="showInfo" />
- 系统修饰键(用法特殊):
ctrl、alt、shift、meta
(1)配合keyup使用:按下修饰键的同时,再按下其他键(任意),随后释放其他键,事件才被触发。
eg:ctrl键:按下ctrl + y,之后松开y,事件就可以触发了。
<input type="text" placeholder="按下回车提示输入" @keyup.ctrl="showInfo" />
如果想要指定ctrl+y才能触发事件,可以在ctrl后面添加.y
<input type="text" placeholder="按下回车提示输入" @keyup.ctrl.y="showInfo" />
(2)配合keydown使用:正常触发事件。
4. 也可以使用keyCode
去指定具体的按键(不推荐)
<input type="text" placeholder="按下回车提示输入" @keyup.13="showInfo" />
Vue.config.keyCodes.自定义键名=键码
,可以去定制按键别名
<input type="text" placeholder="按下回车提示输入" @keyup.huiche="showInfo" />
<script type="text/javascript">
Vue.config.keyCodes.huiche=13
</script>
事件修饰符
Vue中的事件修饰符:
1.prevent:
阻止默认事件(常用);用法@事件名.prevent=“函数名”
2.stop
:阻止事件冒泡(常用);
3.once
:事件只触发一次(常用);
4.capture
:使用事件的捕获模式;
5.self
:只有event.target是当前操作的元素是才触发事件;
6.passive
:事件的默认行为立即执行,无需等待事件回调执行完毕;
默认情况下是先只想玩事件回调函数,再执行默认行为,这样就会出现一个问题,如果回调函数非常复杂,默认行为的执行就显得很卡顿,所以可以通过passive解决问题。
7. 修饰符可以连续写。
eg:如果想要a标签点击之后既不冒泡也不跳转,就可以:@click.prevent.stop=“函数名”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="../js/vue.js"></script>
<style>
*{
margin: 20px;
}
#demo1{
background-color: brown;
width: 100px;
height: 100px;
}
.box1{
background-color: gold;
padding: 10px;
width: 100px;
}
.box2{
background-color: rgb(255, 135, 50);
padding: 10px;
}
.list{
width: 200px;
height: 200px;
background-color: coral;
overflow: auto;
}
li{
height: 100px;
}
</style>
</head>
<body>
<div id="root">
<h1>欢迎,{{name}}</h1>
<!-- 1.`prevent:`阻止默认事件 -->
<a href="https://www.baidu.com/" @click.prevent="showInfo">点我进行跳转</a>
<!-- 2.stop:阻止事件冒泡(常用); -->
<div id="demo1" @click="showInfo">
<button @click.stop="showInfo">点我提示信息</button>
</div>
<!-- 3.once:事件只触发一次(常用); -->
<button @click.once="showInfo">点我提示信息</button>
<!-- 4.capture:使用事件的捕获模式; -->
<div class="box1" @click.capture="showMsg(1)">
div1
<div class="box2" @click="showMsg(2)">
div2
</div>
</div>
<!-- 5.self:只有event.target是当前操作的元素是才触发事件; -->
<!-- 某种程度上可以解决冒泡 -->
<div id="demo1" @click.self="showInfo">
<button @click.self="showInfo">点我提示信息</button>
</div>
<!-- 6.passive:事件的默认行为立即执行,无需等待事件回调执行完毕; -->
<ul class="list" @wheel.passive="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
<script type='text/javascript'>
Vue.config.productionTip = false;
new Vue({
el:'#root',
data: {
name:"yang"
},
methods:{
showInfo(event){
console.log(event.target)
alert("hello")
},
showMsg(number){
alert(number)
},
demo(){
console.log("滚动了")
for (var i = 0; i < 100000; i++) {
console.log("#")
}
}
}
})
</script>
</body>
</html>