js復制內(nèi)容到剪貼板,代碼如下:
function copy (text) {
// text是復制文本
// 創(chuàng)建input元素
const el = document.createElement('input')
// 給input元素賦值需要復制的文本
el.setAttribute('value', text)
// 將input元素插入頁面
document.body.appendChild(el)
// 選中input元素的文本
el.select()
// 復制內(nèi)容到剪貼板
document.execCommand('copy')
// 刪除input元素
document.body.removeChild(el)
alert('復制成功')
}