查阅网上的资料后发现大多是使用插件,可是项目只有一处页面用到了该功能,所以不考虑插件的实现
原生方法 document.execCommand('Copy')
参考:https://segmentfault.com/q/10...
successful返回的结果为 true 但是功能不生效
html部分代码
已解决
悬赏分:40
- 解决时间 2021-11-28 19:18
点赞 0反对 0举报 0
收藏 0
分享 0
回答2
最佳
-
生成一个 textarea 赋值复制方便多啦
doCopy(text, successInfo = '复制成功', failInfo = '复制失败') { let textArea = document.createElement('textarea'); textArea.value = text; document.body.appendChild(textArea); textArea.select(); try { const successful = document.execCommand('copy'); if (!successful) throw new Error(failInfo); this.$Message.success({ content: successInfo }); } catch (err) { this.$Message.error({ content: failInfo }); } document.body.removeChild(textArea); textArea = null; },
支持 0 反对 0 举报2021-11-28 11:39