JavaScript 实现输入框内容一键复制(附上 Vue 3 实现方式)

一、JS 实现方式

实现代码:

<input type="text" />
<button>一键复制</button>
const inputText = document.getElementsByTagName("input")[0];
const copyButton = document.getElementsByTagName("button")[0];

copyButton.addEventListener("click", () => {
  inputText.select();
  document.execCommand("copy");
});

二、Vue3 实现方式

实现代码:

<input type="text" ref="inputText"/>
<button @mousedown="clickCopy">一键复制</button>
const inputText = ref({} as unknown);
const clickCopy = () => {
  (inputCode.value as HTMLInputElement).select();
  document.execCommand("copy");
};
原文地址:https://www.cnblogs.com/Leophen/p/13747116.html