import { message } from 'antd';
export function handleCopyValue(text:string) {
//浏览器禁用了非安全域的 navigator.clipboard 对象
//在线上环境会报错 TypeError: Cannot read properties of undefined (reading 'writeText')
if (navigator.clipboard && window.isSecureContext) {
message.success('复制成功!')
return navigator.clipboard.writeText(text)
} else {
const textArea = document.createElement('textarea')
textArea.value = text
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
message.success('复制成功')
return new Promise((res, rej) => {
// 执行复制命令并移除文本框
document.execCommand('copy') ? res() : rej()
textArea.remove()
})
}
}
js 复制文本
最新推荐文章于 2024-04-12 18:31:57 发布