有时需要实现将内容复制到剪贴板的功能,该方法实现了安卓与ios的兼容
function copy(data){
var weixin = data;
var tmpInput = document.createElement('input');
tempInput.value = weixin;
document.body.appendChild(tempInput );
if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
window.getSelection().removeAllRanges();
var range = document.createRange();
range.selectNode(tempInput);//选中需要复制的节点
window.getSelection().addRange(range);
var successful = document.execCommand('copy');
window.getSelection().removeAllRanges();
}else{
tempInput.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
tempInput.className = 'tempInput ';
tempInput.style.display='none';
document.body.removeChild(tempInput );//移除
layui.use('layer',function(){
const layer = layui.layer;
layer.msg('复制成功')
})
}
})