說明
最近在學小程序,遇到長按圖片刪除的問題,特此記錄,記錄自己的成長軌跡
需求:
長按刪除指定圖片
需要解決的問題
解決辦法
具體實現
<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}}" 來綁定事件并添加索引index
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 }); } }) }
刪除部分的代碼
注意currentTarget與target的區別
1. currentTarget:綁定的事件當前元素及其子元素都會觸發
2. target: 綁定的事件 子元素不會被觸發事件
效果展示
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答