微信小程序使用bindtap事件data-xxx传值无法获取

作者 : admin 本文共415个字,预计阅读时间需要2分钟 发布时间: 2024-06-9 共2人阅读

文章目录

    • 1.实例代码
    • 2.原因分析
    • 3.解决办法

1.实例代码

index.wxml


 <view data-hi="数据1" bindtap="menuTouch">
    <image mode="aspectFit" src="{{item.src}}"></image>
    <text class="menu-item-text">{{item.text}}</text>
  </view>

index.js

menuTouch(e){
  console.log(e.target.dataset)
},

现象:点击view触发menuTouch事件,在.js中打印e.target.dataset,没有得到hi的数据。

2.原因分析

currentTarget指向事件所绑定的元素,target始终指向事件发生时的元素

3.解决办法

通过currentTarget属性获取数据

menuTouch(e){
  console.log(e.currentTarget.dataset)
},
本站无任何商业行为
个人在线分享 » 微信小程序使用bindtap事件data-xxx传值无法获取
E-->