<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>