JS实现文本复制功能
function copy (text) {
let text = text,
textarea,
copyText = '';
if (!text) {
alert('亲,复制的内容不能是空哦~~');
return;
}
textarea = document.createElement('textarea');
textarea.value = text;
textarea.readOnly = 'readonly';
// textarea.style.display = 'none'; //这样会拿不到复制的信息
textarea.style.cssText = 'opacity:0;position:fixed;left:-9999px;';
document.body.appendChild(textarea);
textarea.select();
copyText = document.execCommand('Copy');
if (copyText) {
alert('亲,内容已经复制到剪切板中了哦~~');
}
document.body.removeChild(textarea);
}