问题:
由于新版浏览器的安全策略,clipboard只有在安全域名下才可以访问,http域名下会显示undefined,但使用https开头的域名,或localhost,就可以访问navigator.clipboard
解决:
function fnCopy(copyText) {
navigator.clipboard
.writeText(copyText)
.then(() => {
console.log('复制成功')
})
.catch(() => {
const input = document.createElement('input')
document.body.appendChild(input)
input.setAttribute('value', copyText)
input.select()
if (document.execCommand('copy')) {
document.execCommand('copy')
}
document.body.removeChild(input)
console.log('复制成功')
})
}