The applet event is delegated to the parent element scrollview, but the bound dataset value of the child element view cannot be obtained

The click event is delegated to the parent element: scrollview reduces multiple loop bindings:
it is judged which child element is clicked through the dataset of the click event.

You can often see e.target or e.currentTarget.
Simply put, currentTarget is the current object, and target is the entire object (including child elements).
So when a single component is clicked, currentTarget can be used to directly obtain its data, and when the component is wrapped in a layer, the event is in the parent element, and the data is in the child element, you need to use target


question:

But in the scrollview, the data-xxx attribute bound to the first-level sub-element view cannot be obtained.

Solution:
Use the pointer-events attribute of css
pointer-events: none; // The element will never trigger a click event, it will bubble down

//wxml

.title,.money, .second {
  pointer-events:none;
}

<scroll-view class="left" hidden="{
   
   {!leftType}}" bindtap="toDetail" data-left>
        <view wx:for="{
   
   {lostList.data}}" wx:for-item="pay"  class="pay-list-item" data-refno="{
   
   {pay.REFNO}}">
            <text class="title" data-count>丢失数量:{
   
   {pay.Count}}</text>
            <view class="money">
                <text class="{
   
   {pay.Status=='取消'?'cl89':''}}" style="point-event:none">- {
   
   {pay.Amount}}</text>
            <view class="flex_row second" >
                <text class="time">{
   
   {pay.Time}}</text>
                <text class="pay {
   
   {pay.Status=='取消'?'cl89':'cl50'}}">{
   
   {pay.Status}}</text>
            </view>
        </view>
</scroll-view>

 This click event bindtap='toDetal' can get the data-refno dataset

 .title,.money, .second {
  pointer-events:none;
}

 Effect: No matter where you click on this subview, you can get refno

 

Comments with reference links:

Event delegation is used in the applet. When there are multiple elements in the sub-level, click the attribute in the dataset to be empty? | WeChat Open Community

Guess you like

Origin blog.csdn.net/LlanyW/article/details/131561422
Recommended