vue3点击按钮复制文本功能

<input ref="textCopy" value="" style="opacity:0;position:absolute" />
<button @click="textCopy">复制</button>

let textCopy = ref();
const textCopy = () => {
  const text = '内容'; // 复制文本内容
   const input = textCopy.value;
  input.value = text; // 修改input的内容
  input.select(); // 选中文本
  document.execCommand("copy"); // 浏览器复制
  alter("复制成功");
};

注意:input不能直接隐藏,要不没效果,可以用透明度来实现在页面看不到效果。

猜你喜欢

转载自blog.csdn.net/qq_41697998/article/details/128039577