v-on

.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 />

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VKJQoKvN-1604461013847)(C:\Users\pmq0102\AppData\Roaming\Typora\typora-user-images\image-20201104093146288.png)]

效果:无论怎么点击复选框勾选不上

:keypress 键盘按下事件

html:

<!-- 键盘事件 -->
<input type="text"  v-on:keypress="keyPress($event)" />

vue:

methods:{				
					keyPress(e){
						console.log(e.keyCode)
					}
				}

效果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-miIZ2Szu-1604461013850)(C:\Users\pmq0102\AppData\Roaming\Typora\typora-user-images\image-20201104093806215.png)]

.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>

效果:点击相应颜色的按钮,字体颜色随之相应改变

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值