vue子元素綁定的事件, 阻止觸發(fā)父級(jí)上的事件處理方式
vue子元素綁定的事件,阻止觸發(fā)父級(jí)上的事件處理
index1.html
<html>
<head>
<style>
.parent{
width:200px;
background:#ffc;
}
.child{
list-style: none;
border:1px solid red;
}
</style>
<script src="../../libs/vue.js"></script>
<script>
/*
該用例 li的點(diǎn)擊事件, 在li本身綁定的事件發(fā)生完, 會(huì)觸發(fā)父級(jí)ul上綁定的事件(冒泡)
index2-4 將會(huì)用3種方式進(jìn)行解決
*/
</script>
</head>
<div id="app">
<ul class="parent" @click="parent">
<li class="child" @click="child">1</li>
<li class="child" @click="child">2</li>
<li class="child" @click="child">3</li>
</ul>
</div>
<script>
var App = new Vue({
el:"#app",
data:{},
methods:{
parent(){
alert('this is parent')
},
child(){
alert('this is child')
}
}
})
</script>
</html>預(yù)覽效果:

index2.html
<html>
<head>
<style>
.parent{
width:200px;
background:#ffc;
}
.child{
list-style: none;
border:1px solid red;
}
</style>
<script src="../../libs/vue.js"></script>
<script>
/*
解決方法一 :
在vue級(jí)子級(jí)元素綁定事件用阻止冒泡, @click.stop="fn"
*/
</script>
</head>
<div id="app">
<ul class="parent" @click="parent">
<li class="child" @click.stop="child">1</li>
<!--在子元素中,綁定一個(gè)阻止冒泡的點(diǎn)擊事件 @click.stop-->
<li class="child" @click.stop="child">2</li>
<li class="child" @click.stop="child">3</li>
</ul>
</div>
<script>
var App = new Vue({
el:"#app",
data:{},
methods:{
parent(){
alert('this is parent')
},
child(){
alert('this is child')
}
}
})
</script>
</html>index3.html
<html>
<head>
<style>
.parent{
width:200px;
background:#ffc;
}
.child{
list-style: none;
border:1px solid red;
}
</style>
<script src="../../libs/vue.js"></script>
<script>
/*
解決方式二: vue父級(jí)元素綁定事件, 傳入$event參數(shù)
在父級(jí)綁定的件事處理方法中, 進(jìn)行判斷event.currentTarget 與event.target 是不是全等
如果全等, 就說(shuō)明是父級(jí)上觸發(fā)的點(diǎn)擊事件
event.currentTarget -- 綁定事件的dom
event.target -- 當(dāng)前點(diǎn)擊的dom
*/
</script>
</head>
<div id="app">
<ul class="parent" @click="parent($event)">
<li class="child" @click="child">1</li>
<li class="child" @click="child">2</li>
<li class="child" @click="child">3</li>
</ul>
</div>
<script>
var App = new Vue({
el:"#app",
data:{},
methods:{
parent(event){
let event1 = event.currentTarget;
let event2 = event.target;
if(event1 == event2) {
alert('this is parent')
}
},
child(){
alert('this is child')
}
}
})
</script>
</html>index4.html
<html>
<head>
<style>
.parent{
width:200px;
background:#ffc;
}
.child{
list-style: none;
border:1px solid red;
}
</style>
<script src="../../libs/vue.js"></script>
<script>
/*
解決方式三: 子元素綁定事件傳入$event , 處理器中進(jìn)行阻止冒泡傳遞
event.stopPropagation();
*/
</script>
</head>
<div id="app">
<ul class="parent" @click="parent">
<li class="child" @click="child($event)">1</li>
<li class="child" @click="child($event)">2</li>
<li class="child" @click="child($event)">3</li>
</ul>
</div>
<script>
var App = new Vue({
el:"#app",
data:{},
methods:{
parent(){
alert('this is parent')
},
child(event){
alert('this is child');
event.stopPropagation();
}
}
})
</script>
</html>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VUE watch監(jiān)聽(tīng)器的基本使用方法詳解
這篇文章主要介紹了vue使用watch監(jiān)聽(tīng)器的基本使用方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
vue安裝less-loader依賴失敗問(wèn)題及解決方案
這篇文章主要介紹了vue安裝less-loader依賴失敗問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue中使用v-if隱藏元素時(shí)會(huì)出現(xiàn)閃爍問(wèn)題的解決
這篇文章主要介紹了vue中使用v-if隱藏元素時(shí)會(huì)出現(xiàn)閃爍問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
elementUI實(shí)現(xiàn)下拉選項(xiàng)加多選框的示例代碼
因產(chǎn)品需求和UI樣式調(diào)整,本文主要實(shí)現(xiàn)elementUI下拉選項(xiàng)加多選框的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Vue3進(jìn)階主題Composition API使用詳解
這篇文章主要為大家介紹了Vue3進(jìn)階主題Composition API使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼
本篇文章主要介紹了vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
vue-cli3.0修改打包后的文件名和文件地址,打包后本地運(yùn)行報(bào)錯(cuò)解決
這篇文章主要介紹了vue-cli3.0修改打包后的文件名和文件地址,打包后本地運(yùn)行報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue中定義全局聲明vscode插件提示找不到問(wèn)題解決
這篇文章主要為大家介紹了vue中定義全局聲明vscode插件提示找不到問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
vue3.0 Reactive數(shù)據(jù)更新頁(yè)面沒(méi)有刷新的問(wèn)題
這篇文章主要介紹了vue3.0 Reactive數(shù)據(jù)更新頁(yè)面沒(méi)有刷新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
詳解關(guān)于Vuex的action傳入多個(gè)參數(shù)的問(wèn)題
這篇文章主要介紹了詳解關(guān)于Vuex的action傳入多個(gè)參數(shù)的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02

