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");
};
posted @ 2020-08-25 21:48  Leophen  阅读(651)  评论(0编辑  收藏  举报