- vue阻止事件冒泡 两种方法:
- 使用@click.stop = "show()"
- 方法里面写e.stopPropagation()
show: function (e) {
alert("this is 3");
e.stopPropagation(); //阻止
}
<!--阻止事件默认方法-->
<button @click.prevent.stop="show()">text</button>
- 关于@click.stop和@click.prevent
<div id="app">
<div v-on:click="dodo">
<button v-on:click="doThis">阻止单击事件继续传播</button>
</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
name: "Vue.js"
},
methods: {
doThis: function () {
alert("noclick");
},
dodo: function () {
alert("dodo");
}
}
});
</script>
//将会先弹出“noclick”,再弹出“dodo”。
<div id="app">
<div v-on:click="dodo">
<button v-on:click.stop="doThis">阻止单击事件继续传播</button>
</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
name: "Vue.js"
},
methods: {
doThis: function () {
alert("noclick");
},
dodo: function () {
alert("dodo");
}
}
});
</script>
//只弹出“noclick”
<a href="http://www.baidu.com" @click.prevent="test4">百度一下</a> //阻止a标签跳转,仅执行函数test4
<form action="/xxx" @submit.prevent="test5"> //阻止表单提交,仅执行函数test5
<input type="submit" value="注册">
</form>
- 按键修饰符
<input type="text" @keyup.enter="test7">
methods: {
test7 (event) {
console.log(event.keyCode)
alert(event.target.value)
}
}