137-3385-8848(同微信)

微信小程序长按删除图片的实现

2022-04-21 管理员

小程序实现长按删除指定图片需要如何处理实现,下面为大家介绍实现长按删除指定图片的操作流程。

 

小程序实现长按删除指定图片需要解决的问题

长按事件如何表示出来?
如何获取当前长按元素的下标?
如何删除元素?

小程序实现长按删除指定图片解决办法

长按事件是用bindlongpress(不会跟点击事件bindtap冲突);

在wxml中添加索引index,然后在js中用currentTarget.dataset.index获取当前元素下标

通过splice方法删除splice(index,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.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的区别

currentTarget:绑定的事件当前元素及其子元素都会触发
target: 绑定的事件 子元素不会被触发事件

 

 
上一篇:wx.getUserInfo(OBJECT),小程序获取用户信息
下一篇:微信小程序封装组件的实现,小程序如何封装组件
相关资讯 Releva ntnews
解决方案 Solutions
相关热点 Hot spot
郑州做排名中正确使用关键词优化的方法
  1. 我们的优势
  2. 我们的实力
  3. 选择我们的理由
咨询电话(微信同号)

137-3385-8848(同微信)

豫ICP备16014343号-1

Copyright © 2017-2022 版权所有 酷微科技 Rights Reserved

电话咨询 在线咨询 服务项目 SEO优化