1、添加点击事件
<span class="copyMa" @click="copy()">点击复制</span>
2、copy的实现方法(单行文本)
copy(){
let random = this.$refs.randomCodeRef.innerHTML;
const input = document.createElement('input');
document.body.appendChild(input);
input.setAttribute('value',random);
input.setAttribute('create',1);
input.select();
document.execCommand("Copy");
var list=document.getElementsByTagName('input')
var inputList = Array.prototype.slice.call(list)
inputList.forEach((item)=>{
if(item.getAttribute('create'))document.body.removeChild(item);
});
},
3、copy的实现方法(多行文本)
//如下换行设置 \r\n
copy(){
let copyText = "分享链接:\r\n"+this.shareQrcodeUrl+"\r\n";
copyText = copyText +"分享码:"+this.shareCode+"\r\n";
const input = document.createElement('textarea');
document.body.appendChild(input);
input.innerHTML = copyText;
input.setAttribute('code',1);
input.select();
document.execCommand("Copy");
var list = document.getElementsByTagName('textarea');
var inputList = Array.prototype.slice.call(list);
inputList.forEach((item)=>{
if(item.getAttribute('code'))document.body.removeChild(item);
});
},