微信小程序-點擊復制功能


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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM