vue 点击复制到粘贴板

点击复制功能其实也是模拟 手动选中后再复制的动作

1) 先创建一个input节点

 const input = document.createElement('input')
 document.body.appendChild(input)

2) 给input赋值

input.setAttribute('value','aaa')

3)选中input框内的数值

input.select()

4) 执行浏览器的复制功能

document.execCommand('copy')

5)移除input 同时提醒复制成功

document.body.removeChild(input)
this.$Message.info('复制成功!')

代码:

const input = document.createElement('input')
document.body.appendChild(input)
input.setAttribute('value','aaa')
input.select()
document.execCommand('copy')
document.body.removeChild(input)
this.$Message.info({content:'复制成功!',duration:2})
发布了38 篇原创文章 · 获赞 5 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_39423672/article/details/103711792