vue通过原生事件复制文本(兼容ios)
handleCopyName() {
let str = this.temp.part1.realname
this.copy(str)
},
copy(str) {
const save = (e) => {
e.clipboardData.setData('text/plain', str)
e.preventDefault()
}
document.addEventListener('copy', save)
document.execCommand('copy')
document.removeEventListener('copy', save)
this.$message.success('复制成功')
}
补充:兼容ios
<p @click="handleCopy" id="copyBT">{{text.message}}</p> <i @click="handleCopy">点击链接复制</i>
handleCopy() { const range = document.createRange() range.selectNode(document.getElementById('copyBT')) const selection = window.getSelection() if (selection.rangeCount > 0) selection.removeAllRanges() selection.addRange(range) document.execCommand('copy') this.$toast('复制成功') }