js 自动复制操作
Hkc 2018-03-25 js
<button id="btn">copy</button>
var Button = document.getElementById('btn')
Button.addEventListener('click', (event) => {
let sometext = '内容',
hiddenInput = document.createElement('input')
hiddenInput.value = sometext
hiddenInput.setAttribute('readonly', '')
hiddenInput.style.position = 'absolute'
hiddenInput.style.left = '-9999px'
document.body.appendChild(hiddenInput)
hiddenInput.select()
hiddenInput.setSelectionRange(0, hiddenInput.value.length) // ios
document.execCommand('copy')
document.body.removeChild(hiddenInput)
alert('已复制')
})