js实现一键复制文字

 原生方法:

const target = document.createElement('input') // 创建input节点
      target.value = key // 给input的value赋值(要复制的文字)
      document.body.appendChild(target) // 向页面插入input节点
      target.select() // 选中input
      try {
        await document.execCommand('Copy') // 执行浏览器复制命令
        console.log('复制成功')
      } catch {
        console.log('复制失败')
      }

你可能感兴趣的:(前端学习,javascript,前端,开发语言)