SoFunction
Updated on 2025-03-08

Summary of the difference between target and currentTarget in WeChat applet

Preface

In applets, it is very important, especially when it comes to page transfer values.

CurrentTarget and target are both sets of attribute values ​​of components, and some attribute values ​​defined by "data-attribute name"

currentTarget: The current event triggered by the event (the current event may be the source component that triggers the event, or the event component that triggers the event (that is, the child element that triggers the event source component). At this time, clicking the child element or the parent element is both the current event, and the application
target: The source component that triggers the event (the component where the event is registered/binding)

For example:

<view bindtap="parentClick" data-parent="Parent Element">
<view data-child="Sub-element">edit</view> 
 </view>

Then click on the child element in the parent element:

The currentTarget and target in the parentClick event are printed out, as follows:

parentClick:function(e){
("Click on the parent element")
("target",)//target:{child:"child element"}("currentTarget",)//target:{parent:"parent element"}}

Because the parentClick event bound to the parent element is triggered by the child element, the value of the target is the set of some attribute values ​​contained in the child element; currentTarget is the set of attribute values ​​of the component (parent element) of the binding event.

Bind an event to the child element: childClick, the printed currentTarget and target are the same.

<view bindtap="parentClick" data-parent="Parent Element">
<view bindtap="childClick" data-child="Sub-element">edit</view> 
 </view>

Then click on the child element event:

childClick:function(e){
("Click on the parent element")
("target",)//target:{child:"child element"}("currentTarget",)//target:{child:"child element"}}

Extended

Combined with this:

  • this === is always true
  • This === It may not be true

This is the article about the difference between target and currentTarget in WeChat applets. For more information about the differences between target and currentTarget in WeChat applets, please search for my previous articles or continue to browse the related articles below. I hope everyone will support me in the future!