1. 程式人生 > >原生js實現點擊復制功能

原生js實現點擊復制功能

原生js實現點擊復制功能

  • 代碼:
    <input type="text">
    <button>復制</button>
    <script>
    var input = document.getElementsByTagName(‘input‘)[0]
    var button = document.getElementsByTagName(‘button‘)[0]
    button.onclick = function(){
        input.value = "hello world"
        input.select()
        document.execCommand("copy")
    }
    </script>
  • 效果:
    技術分享圖片
    技術分享圖片
  • execCommand常用方法:
    cut
    delete
    copy
  • 原生js實現點擊復制功能