JS原生-浏览器-复制内容到剪切板


  let btn = document.querySelector('.btn')

  const copyToCliboard = (text) => {
    if (navigator.clipboard) {
      navigator.clipboard.writeText(text)
    } else {
      var textarea = document.createElement('textarea')
      textarea.value = text
      document.body.appendChild(textarea)
      textarea.select()
      textarea.setSelectionRange(0, textarea.value.length)
      document.execCommand('copy')
      document.body.removeChild(textarea)
    }
  }

  btn.addEventListener('click', function () {
    copyToCliboard('当你点击的时候,就已经复制这句话了')
  })

你可能感兴趣的:(javascript,html)