vue常用事件v-on:click詳解事件對象,事件冒泡,事件默認(rèn)行為
其實(shí)v-on后面跟的不止是click事件也可以是其他的事件,用法均相似。比如:v-on:click/mouseout/mouseover/mousedown.......
以下click為例
注意:所有的v-on都可以簡寫為@,比如說v-click可以簡寫為@click
1.監(jiān)聽事件
可以用v-on指令監(jiān)聽 DOM 事件,并在觸發(fā)時(shí)運(yùn)行一些 JavaScript 代碼。通常來講就是監(jiān)聽dom觸發(fā)一些操作,這些操作(比如點(diǎn)擊)觸發(fā)后執(zhí)行的動作(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ū)懺诶锩婢蜁斐苫靵y,視圖和邏輯混淆。所以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>效果一致。對方法的調(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.事件對象
$event 拿到當(dāng)前點(diǎn)擊事件的事件對象,比如click就是拿到當(dāng)前點(diǎn)擊的dom事件對象信息
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í)候會彈兩次
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>那么但阻止冒泡后就只會彈一次
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)行為:
比如:如下右鍵之后會將默認(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)菜單將不會顯示(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)的代碼會以同樣的順序產(chǎn)生。因此,用@click.prevent.self會阻止所有的點(diǎn)擊,而@click.self.prevent只會阻止對元素自身的點(diǎn)擊。
2.1.4 新增
<!-- 點(diǎn)擊事件將只會觸發(fā)一次 --> <a v-on:click.once="doThis"></a>
不像其它只能對原生的 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修飾符來提升移動端的性能。舉個(gè)例子,在滾動的時(shí)候,瀏覽器會在整個(gè)事件處理完畢之后再觸發(fā)滾動,因?yàn)闉g覽器并不知道這個(gè)事件是否在其處理函數(shù)中被調(diào)用了event.preventDefault()。.passive修飾符用來進(jìn)一步告訴瀏覽器這個(gè)事件的默認(rèn)行為不會被取消。
不要把.passive和.prevent一起使用。被動處理函數(shù)無法阻止默認(rèn)的事件行為。
補(bǔ)充:原生JS阻止冒泡
來自網(wǎ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以及事件對象,事件冒泡,事件默認(rèn)行為的文章就介紹到這了,更多相關(guān)vuev-on:click內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3中如何修改父組件傳遞到子組件中的值(全網(wǎng)少有!)
大家都知道,vue是具有單向數(shù)據(jù)流的傳遞特性,下面這篇文章主要給大家介紹了關(guān)于Vue3中如何修改父組件傳遞到子組件中值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
微信小程序Echarts動態(tài)使用及圖表層級踩坑解決方案
這篇文章主要為大家介紹了微信小程序Echarts動態(tài)使用及圖表層級踩坑解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
詳解vue2.0 使用動態(tài)組件實(shí)現(xiàn) Tab 標(biāo)簽頁切換效果(vue-cli)
本篇文章主要介紹了詳解vue2.0 使用動態(tài)組件實(shí)現(xiàn) Tab 標(biāo)簽頁切換效果(vue-cli),具有一定的參考價(jià)值,有需要的可以了解下2017-08-08
Vue服務(wù)端渲染實(shí)踐之Web應(yīng)用首屏耗時(shí)最優(yōu)化方案
這篇文章主要介紹了Vue服務(wù)端渲染實(shí)踐之Web應(yīng)用首屏耗時(shí)最優(yōu)化方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Pinia入門學(xué)習(xí)之實(shí)現(xiàn)簡單的用戶狀態(tài)管理
Vue3雖然相對于Vue2很多東西都變了,但是核心的東西還是沒有變,比如說狀態(tài)管理、路由等,再Vue3中尤大神推薦我們使用pinia來實(shí)現(xiàn)狀態(tài)管理,他也說pinia就是Vuex的新版本,這篇文章主要給大家介紹了關(guān)于Pinia入門學(xué)習(xí)之實(shí)現(xiàn)簡單的用戶狀態(tài)管理的相關(guān)資料,需要的朋友可以參考下2022-11-11
vue中el-tree增加節(jié)點(diǎn)后如何重新刷新
這篇文章主要介紹了vue中el-tree增加節(jié)點(diǎn)后如何重新刷新,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

