小程序中的事件委托

wxml中

<view bindtap="parent">
<view data-name="child1">child1</view>
<view data-name="child2">child2</view>
</view>

js中

 parent(e) {
    
    
    console.log(e)
    if(e.target.dataset.name == 'child1') {
    
    
      console.log('点击的是child1')
    } else if (e.target.dataset.name == 'child2') {
    
    
      console.log('点击的是child2')
    }
  },

################################################################################################
小程序不像vue(传值的话可以在事件后边直接括号,里边写要传的数据),可以通过data-自定义名=“值”,在js中,通过e.target.dataset.自定义名获取传的数据值

猜你喜欢

转载自blog.csdn.net/m0_49159526/article/details/107647528
今日推荐