微信小程序-点击复制功能
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>