js将文本添加到剪贴板上
在本地环境中可以使用navigator.clipboard.writeText 实现在剪贴板中添加文本,但是线上环境会提示找不到writeText 属性,所以需要换一种写法:
剪贴板添加文本兼容写法
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(copyText.value);
} else if (document.execCommand('copy')) {
const textArea = document.createElement('textArea');
(textArea as any).value = copyText.value;
document.body.appendChild(textArea);
textArea.style.position = 'fixed';
textArea.style.left = '0';
textArea.style.top = '0';
(textArea as any)?.focus();
(textArea as any)?.select();
document.execCommand('copy');
document.body.removeChild(textArea);
} else {
// 当前浏览器不支持复制
ElMessage.error('复制失败!');
}
原文地址:https://blog.csdn.net/qq_43225508/article/details/142922160
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!