<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span id="a_1">复制成功</span> <button onclick="copyUrl(1)">复制</button> <script> function copyUrl(id) {
const range = document.createRange();//创建range对象 range.selectNode(document.getElementById('a_'+id)); const selection = window.getSelection();//选择的文本范围 selection.addRange(range);//向选区(Selection)中添加一个区域(Range) document.execCommand('Copy');//将当前选中区复制到剪贴板 console.log("复制成功!"); } </script> </body> </html>