.stop 阻止冒泡行为
html:
<h2>阻止冒泡行为</h2>
<div class="item" @click="click1">
<div class="inside" @click.stop="click2">
<div @click.stop="click3" class="iin"></div>
</div>
</div>
vue :
methods:{
click1(){
alert(1);
},
click2(){
alert(2);
},
click3(){
alert(3);
},
}
效果:点击.iin只会弹3,点击.inside只会弹2,点击item只会弹1
.prevent 阻止默认行为
html:
<!-- 阻止默认事件 -->
<input type="checkbox" @click.prevent />
效果:无论怎么点击复选框勾选不上
:keypress 键盘按下事件
html:
<!-- 键盘事件 -->
<input type="text" v-on:keypress="keyPress($event)" />
vue:
methods:{
keyPress(e){
console.log(e.keyCode)
}
}
效果:
.capture 冒泡时优先处理拥有此属性的指令
html:
<!-- 优先处理被capture选中的事件 -->
<div class="item" @click.capture="click1">
<div class="inside" @click="click2">
<div @click="click3" class="iin"></div>
</div>
</div>
vue:
methods:{
click1(){
alert(1);
},
click2(){
alert(2);
},
click3(){
alert(3);
},
}
效果:
无论点击哪个div,都先弹出1
用vue v-on实现点击相应的按钮,改变字体的颜色
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
.one{
color: red;
}
.two{
color: green;
}
.three{
color: blue;
}
</style>
</head>
<body>
<div id="app">
<button type="button" v-on:click="changeColor('red')">红色</button>
<button type="button" v-on:click="changeColor('green')">绿色</button>
<button type="button" v-on:click="changeColor('blue')">蓝色</button>
<div v-bind:style="{color:colorType}">{{msg}}</div>
<!-- <div v-bind:class="{one:red,two:green,three:blue}">{{msg}}</div> -->
</div>
<script type="text/javascript">
new Vue({
el:'#app',
data:{
msg:'你好',
colorType:'#000',
red:true,
green:true,
blue:false
},
methods:{
changeColor(color){
this.colorType = color;
// if(color == 'red'){
// this.red = true,
// this.green = false,
// this.blue = false
// }else if(color == 'green'){
// this.red = false,
// this.green = true,
// this.blue = false
// }else if(color == 'blue'){
// this.red = false,
// this.green = false,
// this.blue = true
// }
}
}
})
</script>
</body>
</html>
效果:点击相应颜色的按钮,字体颜色随之相应改变