vue中实现浏览器的复制功能

时间:2023-03-08 22:18:41

  vue中实现浏览器的复制功能

点击复制,就可以实现copy

<p class="inline-block">
<span >{{fenxiao.appSecret}}</span>
<span style="color: #0000FF;cursor: pointer" @click="copyAppSecret">复制</span>
</p>
copyAppSecret() {
let createInput = document.createElement("input");
createInput.value = this.fenxiao.appSecret;
document.body.appendChild(createInput);
createInput.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
createInput.style.display = "none";
this.$message({ message: "复制成功", type: "success" });
}, 网上说的那种
let tt=document.getElementById("xxxx")
tt.select(); // 选择对象 
 document.execCommand("Copy"); // 执行浏览器复制命令 
这种不行
vue中实现浏览器的复制功能

文本是没有select方法的,input才有 所以要先创建input元素,在添加值,在赋值,

亲测有效,换成el-input就能行