在小程序的事件回調(diào)觸發(fā)時(shí),會(huì)接收一個(gè)事件對(duì)象,事件對(duì)象的參數(shù)中包含一個(gè)target和currentTarget屬性,接下來說說這二者的區(qū)別。
首先上代碼:
wxml部分:
<view id='tar-father' bindtap='click'> 父組件 <view id='tar-children'>子組件</view> </view>
wxss部分:
#tar-father{ width: 300rpx; height: 300rpx; background-color: skyblue;}#tar-children{ background-color: pink;}
效果圖
js部分:
click: function (event) { console.log(event.target) console.log(event.currentTarget) }
當(dāng)點(diǎn)擊圖中粉色子組件區(qū)域時(shí)的輸出結(jié)果:
event.target 為其子組件,也就是觸發(fā)該事件的源頭組件
event.currentTarget 為事件所綁定的組件
當(dāng)點(diǎn)擊圖中藍(lán)色父組件區(qū)域時(shí)的輸出結(jié)果:
event.target 為父組件,因?yàn)橛|發(fā)的源頭也就是父組件本身
event.currentTarget 始終為事件所綁定的組件
總結(jié):target對(duì)應(yīng)的是觸發(fā)事件的源頭組件,這個(gè)組件有可能是子組件,有可能是父組件,主要是看執(zhí)行動(dòng)作的區(qū)域。而currentTarget始終對(duì)應(yīng)事件所綁定的組件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選