zl程序教程

您现在的位置是:首页 >  APP

当前栏目

微信小程序-点击复制功能

2023-03-07 09:49:29 时间

wxml:

<view class="page">
  <view class="template flex-col" wx:for="{{templateList}}" wx:key="{{templateList}}" wx:for-index='idx'>
    <view class="title">{{item.Title}}</view>
    <view class="content">{{item.TemplateText}}</view>
    <view class="copy flex-center" bindtap='copyBtn' data-idx='{{idx}}'>复制</view>
  </view>
</view>

js:

//点击一键复制
copyBtn: function (e) {
    var that = this;
    //当前索引
    var currentidx = e.currentTarget.dataset.idx;
    console.log(currentidx); 

    wx.setClipboardData({
      //准备复制的数据内容
      data: that.data.templateList[currentidx].TemplateText,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
},

备注:

如果想长按复制,那就在 text 中设置 selectable=”true

<view class="content">
  <text selectable='true' bindlongtap='copyBtn'>
    {{item.TemplateText}}
  </text>
</view>

未经允许不得转载:肥猫博客 » 微信小程序-点击复制功能