vue事件修饰符(once:prev:stop)

2023-02-14,,,,

vue事件修饰符(once:prev:stop)

stop修饰符  效果如下:

当你鼠标在这个div里的时候,x与y的值;会随着鼠标的变化而变化。但是当鼠标放在stopMoving的时候,x与y的值是不会变化的;因为采用了阻止冒泡事件;或者也可以采用我注释了的那个方法。代码如下:

 <template>
<div id="app">
<div class="stopMoving" v-on:mousemove="upadataXY">
{{x}},{{y}}
<!-- <span v-on:mousemove="stopMoving">
stopMoving
<span/>--> <!-- 採用阻止冒泡事件的方法 -->
<span v-on:mousemove.stop>stopMoving</span>
</div>
</div>
</template> <script>
export default {
name: "app",
data() {
return {
age: 30,
x: 0,
y: 0
};
},
methods: {
upadataXY: function(event) {
console.log(event);
this.x = event.offsetX;
this.y = event.offsetY;
},
stopMoving: function(event) {
event.stopPorpagation();
}
}
};
</script> <style>
#app {
text-align: center;
color: #2c3e50;
margin-top: 60px;
} .stopMoving {
width: 300px;
height: 100px;
border: 1px solid red;
text-align: center;
padding-top:80px;
}
</style>

prevent修饰符  效果如下:

这是一个a链接,默认的请款下会跳转页面;但是当你采用prevent的时候,页面弹出一个框之后,就不会再跳转页面。

代码如下:

 <!-- 采用prevent阻止默认的跳转事件 -->
<a v-on:click.prevent="alert()" href="https://www.baidu.com">the new Page</a> // 在methods里面写入这个方法
alert:function(){
alert('啥?你说素素很美?');
}

可以把.prevent去掉看看效果

vue事件修饰符(once:prev:stop)的相关教程结束。

《vue事件修饰符(once:prev:stop).doc》

下载本文的Word格式文档,以方便收藏与打印。