Vue3 click button to copy text function

<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("复制成功");
};

Note: The input cannot be hidden directly, or it has no effect, you can use transparency to make the effect invisible on the page.

Guess you like

Origin blog.csdn.net/qq_41697998/article/details/128039577