函数,复制到script里面直接调用copyToClipboard(复制的内容)
function copyToClipboard(copyValue) {
if(copyValue.length <= 0){
//定时1秒后复制
setTimeout(copyToClipboard, 1000);
return;
}
var textArea = document.createElement(“textarea”);
textArea.style.position = ‘fixed’;
textArea.style.top = ‘0’;
textArea.style.left = ‘0’;
textArea.style.width = ‘2em’;
textArea.style.height = ‘2em’;
textArea.style.padding = ‘0’;
textArea.style.border = ‘none’;
textArea.style.outline = ‘none’;
textArea.style.boxShadow = ‘none’;
textArea.style.background = ‘transparent’;
textArea.value = copyValue;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand(‘copy’);
var msg = successful ? ‘成功复制到剪贴板’ : ‘复制失败浏览器不支持’;
alert(msg);
} catch (err) {
alert(‘复制失败,浏览器不支持’);
}
document.body.removeChild(textArea);
}