方式二,使用execCommand
function copyText(text) {
const input = document.createElement('textarea')
input.value = text
document.body.appendChild(input)
input.focus()
input.select()
document.execCommand('copy')
document.body.removeChild(input)
}
copyText('text');
需要注意:execCommand方法已经被标记为已废弃
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/execCommand
方式二,使用Chrome提供的方法
window.copy("text");