vue常用事件v-on:click詳解事件對(duì)象,事件冒泡,事件默認(rèn)行為
其實(shí)v-on后面跟的不止是click事件也可以是其他的事件,用法均相似。比如:v-on:click/mouseout/mouseover/mousedown.......
以下click為例
注意:所有的v-on都可以簡(jiǎn)寫為@,比如說v-click可以簡(jiǎn)寫為@click
1.監(jiān)聽事件
可以用v-on指令監(jiān)聽 DOM 事件,并在觸發(fā)時(shí)運(yùn)行一些 JavaScript 代碼。通常來講就是監(jiān)聽dom觸發(fā)一些操作,這些操作(比如點(diǎn)擊)觸發(fā)后執(zhí)行的動(dòng)作(js)可有直接寫在后面
v-on:click="item+=1"
eg:
<template>
<div >
<input type="button" value="clickme" v-on:click="item+=1"/>
<div>{{item}}</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
item:1
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>結(jié)果:

可以看見每點(diǎn)擊一次綁定的值就增加1.也就是說可以吧js的操作放在事件觸發(fā)的后面。但是有時(shí)候邏輯太復(fù)雜的時(shí)候?qū)懺诶锩婢蜁?huì)造成混亂,視圖和邏輯混淆。所以click后面可以接一個(gè)方法,把所有處理邏輯的方法封裝在一個(gè)函數(shù)里面click的時(shí)候調(diào)用
2.事件處理方法
v-on:click="greet"
eg;
<template>
<div >
<input type="button" value="clickme" v-on:click="greet"/>
<div>{{res}}</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
name : 1,
res:""
}
},
methods:{
greet: function () {
// `this` 在方法里指向當(dāng)前 Vue 實(shí)例
this.res='Hello ' + this.name + '!';
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>效果:

可以看見點(diǎn)擊之后執(zhí)行了greet方法里面js邏輯
3.帶參數(shù)的時(shí)間綁定方法:
同上,唯一區(qū)別是攜帶了參數(shù)
v-on:click="greet(name)"
<template>
<div >
<input type="button" value="clickme" v-on:click="greet(name)"/>
<div>{{res}}</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
name : 1,
res:""
}
},
methods:{
greet: function (reccept) {
// `this` 在方法里指向當(dāng)前 Vue 實(shí)例
this.res='Hello ' + reccept+1 + '!';
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>效果一致。對(duì)方法的調(diào)用同樣可以一個(gè)方法多處多次的調(diào)用
4.內(nèi)聯(lián)處理器中的方法
也就是說在方法里面調(diào)用其他的方法,這里的其他方法可以是js原生的方法比如阻止冒泡呀等等,也可以是自定義的方法
v-on:click="greet(name,$event)"
eg:
<template>
<div >
<input type="button" value="clickme" v-on:click="greet(name,$event)"/>
<div>{{res}}</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
name : 1,
res:""
}
},
methods:{
greet: function (reccept,event) {
if (reccept===1) this.say()
},
say:function () {
this.res="我調(diào)用了"
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>效果:

5.事件對(duì)象
$event 拿到當(dāng)前點(diǎn)擊事件的事件對(duì)象,比如click就是拿到當(dāng)前點(diǎn)擊的dom事件對(duì)象信息
v-on:click="greet($event)"
eg:
<template>
<div >
<input type="button" value="clickme" v-on:click="greet($event)"/>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
greet: function (ev) {
alert(ev.clientX)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
v-on:click="greet($event)"/>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
greet: function (ev) {
alert(ev.clientX)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
v-on:click="greet($event)"/>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
greet: function (ev) {
alert(ev.clientX)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>效果:

6.事件冒泡
當(dāng)不阻止事件冒泡的時(shí)候會(huì)彈兩次
eg
<template>
<div >
<div @click="show1($event)">
<div @click="show2($event)">點(diǎn)擊我呀</div>
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
show1: function (ev) {
alert(1)
},
show2: function (ev1) {
alert(2)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>那么但阻止冒泡后就只會(huì)彈一次
eg:原生js阻止冒泡
ev1.cancelBubble=true
<template>
<div >
<div @click="show1($event)">
<div @click="show2($event)">點(diǎn)擊我呀</div>
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
show1: function (ev) {
alert(1)
},
show2: function (ev1) {
ev1.cancelBubble=true
alert(2)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>那么vue自己封裝的阻止冒泡方法呢?
@click.stop="show2()"
eg:
<template>
<div >
<div @click="show1()">
<div @click.stop="show2()">點(diǎn)擊我呀</div>
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
show1: function () {
alert(1)
},
show2: function (ev1) {
alert(2)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>7.阻止默認(rèn)行為:
比如:如下右鍵之后會(huì)將默認(rèn)的菜單帶出來
<template>
<div >
<div>
<div @contextmenu="show2()">右鍵點(diǎn)擊我呀</div>
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
show2: function (ev1) {
alert(2)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>效果:

那么就有了阻止默認(rèn)行為
ev1.preventDefault();
eg:
<template>
<div >
<div>
<div @contextmenu="show2($event)">右鍵點(diǎn)擊我呀</div>
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
show2: function (ev1) {
alert(2);
ev1.preventDefault();
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>點(diǎn)擊后默認(rèn)菜單將不會(huì)顯示(PS早360瀏覽器右鍵無效)
vue里面的封裝的阻止默認(rèn)行為的方法:
@contextmenu.prevent="show2()"
eg:
<template>
<div >
<div>
<div @contextmenu.prevent="show2()">右鍵點(diǎn)擊我呀</div>
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
hide : true
}
},
methods:{
show2: function (ev1) {
alert(2);
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>8.其他事件修飾符
用法都一樣就不再贅述
.capture.self.once
<!-- 阻止單擊事件繼續(xù)傳播 --> <a v-on:click.stop="doThis"></a> <!-- 提交事件不再重載頁面 --> <form v-on:submit.prevent="onSubmit"></form> <!-- 修飾符可以串聯(lián) --> <a v-on:click.stop.prevent="doThat"></a> <!-- 只有修飾符 --> <form v-on:submit.prevent></form> <!-- 添加事件監(jiān)聽器時(shí)使用事件捕獲模式 --> <!-- 即元素自身觸發(fā)的事件先在此處處理,然后才交由內(nèi)部元素進(jìn)行處理 --> <div v-on:click.capture="doThis">...</div> <!-- 只當(dāng)在 event.target 是當(dāng)前元素自身時(shí)觸發(fā)處理函數(shù) --> <!-- 即事件不是從內(nèi)部元素觸發(fā)的 --> <div v-on:click.self="doThat">...</div>
使用修飾符時(shí),順序很重要;相應(yīng)的代碼會(huì)以同樣的順序產(chǎn)生。因此,用@click.prevent.self會(huì)阻止所有的點(diǎn)擊,而@click.self.prevent只會(huì)阻止對(duì)元素自身的點(diǎn)擊。
2.1.4 新增
<!-- 點(diǎn)擊事件將只會(huì)觸發(fā)一次 --> <a v-on:click.once="doThis"></a>
不像其它只能對(duì)原生的 DOM 事件起作用的修飾符,.once修飾符還能被用到自定義的組件事件上。如果你還沒有閱讀關(guān)于組件的文檔,現(xiàn)在大可不必?fù)?dān)心。
<!-- the scroll event will not cancel the default scroll behavior --> <div v-on:scroll.passive="onScroll">...</div>
Vue 為這些修飾符額外提供了.passive修飾符來提升移動(dòng)端的性能。舉個(gè)例子,在滾動(dòng)的時(shí)候,瀏覽器會(huì)在整個(gè)事件處理完畢之后再觸發(fā)滾動(dòng),因?yàn)闉g覽器并不知道這個(gè)事件是否在其處理函數(shù)中被調(diào)用了event.preventDefault()。.passive修飾符用來進(jìn)一步告訴瀏覽器這個(gè)事件的默認(rèn)行為不會(huì)被取消。
不要把.passive和.prevent一起使用。被動(dòng)處理函數(shù)無法阻止默認(rèn)的事件行為。
補(bǔ)充:原生JS阻止冒泡
來自網(wǎng)友評(píng)論貢獻(xiàn)~~
event.cancelBubble=true ie9以下;
event.stoppropagation();主流瀏覽器 IE9及以上,
原生JS阻止默認(rèn)事件:
event.preventDefault();主流
event.returnValue = false;IE
到此這篇關(guān)于vue常用事件之v-on:click以及事件對(duì)象,事件冒泡,事件默認(rèn)行為的文章就介紹到這了,更多相關(guān)vuev-on:click內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3如何實(shí)現(xiàn)PDF文件在線預(yù)覽功能
PDF文件在線預(yù)覽的功能相信大家都是有遇到過的,下面這篇文章主要給大家介紹了關(guān)于vue3如何實(shí)現(xiàn)PDF文件在線預(yù)覽功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法
這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法,需要的朋友參考下2018-02-02
vue去掉嚴(yán)格開發(fā),去掉vue-cli安裝時(shí)的eslint或修改配置方式
這篇文章主要介紹了vue去掉嚴(yán)格開發(fā),去掉vue-cli安裝時(shí)的eslint或修改配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue項(xiàng)目部署到Apache服務(wù)器中遇到的問題解決
這篇文章主要介紹了vue項(xiàng)目部署到Apache中遇到的問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
vue項(xiàng)目中實(shí)現(xiàn)多文件上傳功能實(shí)例代碼
我們平時(shí)經(jīng)常做的是上傳文件,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中實(shí)現(xiàn)多文件上傳功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
vue?router?動(dòng)態(tài)路由清除方式
這篇文章主要介紹了vue?router?動(dòng)態(tài)路由清除方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

