微信小程序 长按事件 删除图片
1:页面代码
<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}}" />
</view>
</block>
</view>
2:js代码
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
});
}
})
}
要点:
1:长按事件是用bindlongpress(不会跟点击事件bindtap冲突);