//textToCopy表示需要复制的文字
function copyToClipboard(textToCopy) {
if (navigator.clipboard && window.isSecureContext) {
return navigator.clipboard.writeText(textToCopy);
}
const textArea = document.createElement('textarea');
textArea.value = textToCopy;
textArea.style.position = 'absolute';
textArea.style.opacity = 0;
textArea.style.left = '-999999px';
textArea.style.top = '-999999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise((res, rej) => {
document.execCommand('copy') ? res() : rej();
textArea.remove();
});
}
前端-复制内容到剪切板
最新推荐文章于 2024-07-17 13:47:47 发布