<body>
    <span id="copy1">你好啊!</span>
    <input type="text" value="你好!"id="copy2">
    <!--onClick="CopyAdd()" 点哪里加哪里-->
    <button id="copy3" onClick="CopyAdd()">复制</button>
    <script>
      function CopyAdd(event) {
        const range = document.createRange();
        range.selectNode(document.getElementById('copy2'));//复制对象ID
        const selection = window.getSelection();
        if(selection.rangeCount > 0) selection.removeAllRanges();
        selection.addRange(range);
        document.execCommand('copy');
        alert("复制成功!");
      }
    </script>
  </body>
Last modification:September 19th, 2019 at 05:00 pm
如果觉得我的文章对你有用,请随意赞赏