复制 input 中的数据
copyHashText: function () {
const hashText = document.getElementsByClassName("hashText")[0];
hashText.select(); // 选中文本
document.execCommand( "Copy" ); // 执行浏览器复制命令
this.InitContacts.titleToast("复制成功")
},
复制 div 中的数据
copyHashTextdiv: function () {
let hastText = document.getElementsByClassName("hashText")[0]
let selection = window.getSelection()
let range = document.createRange()
if(selection.rangeCount > 0) selection.removeAllRanges()
range.selectNode(hastText)
selection.addRange(range)
document.execCommand( "Copy" ); // 执行浏览器复制命令
this.InitContacts.titleToast("复制成功")
}
window.getSelection()介绍
createRange()介绍
小程序中复制 view 内容到剪切板
// wxml
<view class="msg">{{orderDetail.order_no}}<view class="middle">|</view><text class="copy" bindtap="copyOrderNo" data-orderNo="{{orderDetail.order_no}}">复制</text></view>
// javascript
copyExpressNo(e){
// console.log("快递单号",e.currentTarget.dataset.expressno)
wx.setClipboardData({
data: e.currentTarget.dataset.expressno,
success: function (res) {
wx.getClipboardData({
//这个api是把拿到的数据放到电脑系统中的
success: function (res) {
wx.showToast({
title: '已复制',
icon: 'none',
duration: 1000
})
}
})
}
})
},