zoukankan      html  css  js  c++  java
  • 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");
    };
    
  • 相关阅读:
    day29 作业
    day 29 线程
    day28 进程
    day27 服务端 和客户端
    day26 作业
    day26 网络编程
    java基础 反射
    python 计时
    mongodb 批量插入唯一索引冲突
    js hook
  • 原文地址:https://www.cnblogs.com/Leophen/p/13747116.html
Copyright © 2011-2022 走看看