copy = () => {
let text = document.getElementById('copyText').innerHTML
let input = document.createElement('input')
input.setAttribute('id', 'copyInput')
input.setAttribute('value', text)
document.getElementsByTagName('body')[0].appendChild(input)
document.getElementById('copyInput').select()
if (document.execCommand('copy')) {
console.log('复制成功')
// 可以提示一个复制成功的弹窗
}
document.getElementById('copyInput').remove()
}
// 下面是在react中的jsx部分
<>
<span id="copyText">12345600</span>
<span onClick={() => copy()}>复制</span>
</>
11-29
03-28
262