1. 程式人生 > 實用技巧 >微信小程式-點選複製功能

微信小程式-點選複製功能

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>