Open artdong opened 3 years ago
function copyText (text) {
let input = document.createElement('input');
input.setAttribute('readonly', 'readonly');
input.setAttribute('value', text);
document.body.appendChild(input);
input.select(); // 不能少
if (!text || !text.length) {
console.log('复制内容不能为空!');
return;
}
input.setSelectionRange(0, text.length);
if (document.execCommand('copy')) {
document.execCommand('copy');
console.log('复制成功!');
} else {
console.log('复制失败!');
}
document.body.removeChild(input);
}
js原生实现拷贝到剪贴板