vue中把字符串复制到剪切板
vue中把字符串复制到剪切板
copyText(text) { var textarea = document.createElement("input");//创建input对象 var currentFocus = document.activeElement;//当前获得焦点的元素 document.body.appendChild(textarea);//添加元素 textarea.value = text; textarea.focus(); if(textarea.setSelectionRange) textarea.setSelectionRange(0, textarea.value.length);//获取光标起始位置到结束位置 else textarea.select(); try { var flag = document.execCommand("copy");//执行复制 } catch(eo) { var flag = false; } document.body.removeChild(textarea);//删除元素 currentFocus.focus(); return flag; }, // 点击复制 handleCopyClick(address, officialAccountId){ let str1 = 'https://personal.tohours.com/dgt/admin/agentCenter/home/env-html' let str2 = '/#/home?channelid=' + officialAccountId + '&address=' + address let str = str1 + str2 let flag = this.copyText(str); //传递文本,项目需要拼接str1和str2,复制的是str alert(flag ? "复制成功!" : "复制失败!"); }