須要解決的問題小程序
解決辦法this
<view class="uploader__files"> <block wx:for="{{images}}" wx:key="{{item.id}}" > <view class="uploader__file" bindlongpress="deleteImage" data-index="{{index}}"> <image mode="aspectFill" class="uploader__img" src="{{item.path}}" /> </view> </block> </view>
在wxml中添加 bindlongpress="deleteImage" data-index="{{index}}" 來綁定事件並添加索引indexspa
deleteImage: function (e) { var that = this; var images = that.data.images; var index = e.currentTarget.dataset.index;//獲取當前長按圖片下標 wx.showModal({ title: '提示', content: '肯定要刪除此圖片嗎?', success: function (res) { if (res.confirm) { console.log('點擊肯定了'); images.splice(index, 1); } else if (res.cancel) { console.log('點擊取消了'); return false; } that.setData({ images }); } }) }
刪除部分的代碼code
注意currentTarget與target的區別xml
1. currentTarget:綁定的事件當前元素及其子元素都會觸發 2. target: 綁定的事件 子元素不會被觸發事件