1.事件对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<button type="button" @click="change($event)">一个按钮</button>
</div>
</body>
<script type="text/javascript">
new Vue({
el:'#box',
data:{
msg:'hello'
},
methods:{
change(e){
e.target.style.background="red";
}
}
})
</script>
</html>
2.使用事件对象阻止冒泡
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
#wai{
height: 200px;
width: 200px;
background: red;
}
</style>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<div id="wai" @click="wai($event)">
<button @click="nei($event)" type="button">按钮</button>
</div>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
methods:{
wai(e){
e.stopPropagation();
alert("外")
},
nei(e){
e.stopPropagation();
alert("内")
}
}
})
</script>
</html>
3.使用vue的方式来阻止事件冒泡
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
#wai{
height: 200px;
width: 200px;
background: red;
}
</style>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
。
<div id="wai" @click.stop="wai()">
<button @click.stop="nei()" type="button">按钮</button>
</div>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
methods:{
wai(){
alert("外")
},
nei(){
alert("内")
}
}
})
</script>
</html>
4.阻止元素的默认行为
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
#wai{
height: 200px;
width: 200px;
background: red;
}
</style>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<a href="http://www.163.com" @click="intoPage($event)">进入网易</a>
<form action="123.html" method="get" @submit="tijiao($event)">
<input type="text" name="username" id="" value="" />
<input type="submit" value="提交表单"/>
</form>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
methods:{
intoPage(e){
e.preventDefault();
alert("执行点击事件");
},
tijiao(e){
e.preventDefault();
alert("异步提交");
}
}
})
</script>
</html>
5.使用vue的方式阻止元素的默认行为
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
#wai{
height: 200px;
width: 200px;
background: red;
}
</style>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<a href="http://www.163.com" @click.prevent="intoPage()">进入网易</a>
<form action="123.html" method="get" @submit.prevent="tijiao()">
<input type="text" name="username" id="" value="" />
<input type="submit" value="提交表单"/>
</form>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
methods:{
intoPage(){
alert("执行点击事件");
},
tijiao(){
alert("异步提交");
}
}
})
</script>
</html>
6.让事件执行一次
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<button type="button" @click.once="show()">一个按钮</button>
</div>
</body>
<script type="text/javascript">
new Vue({
el:'#box',
data:{
msg:'hello'
},
methods:{
show(){
alert("弹出一次");
}
}
})
</script>
</html>
7.键盘事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<input type="text" name="" id="" value="" @keypress="show($event)"/>
</div>
</body>
<script type="text/javascript">
new Vue({
el:'#box',
data:{
msg:'hello'
},
methods:{
show(e){
if(e.keyCode==13){
alert("开始搜索");
}
}
}
})
</script>
</html>
8.用vue的方式来监听键盘事件某个键
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<input type="text" name="" id="" value="" @keypress.enter="show($event)" />
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
msg: 'hello'
},
methods: {
show() {
alert("开始搜索");
}
}
})
</script>
</html>
9.鼠标事件的按键修饰符
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="box">
<button type="button" @mousedown.left="show($event)">一个按钮</button>
</div>
</body>
<script type="text/javascript">
new Vue({
el:'#box',
data:{
msg:'hello'
},
methods:{
show(e){
alert("鼠标按下");
}
}
})
</script>
</html>