方式二,使用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");
参考 复制文本到粘贴板的三种实现方法(chrome) JS怎么实现在chrome中复制到剪贴板