code
<!DOCTYPE html> <head> <meta charset="UTF-8"> <title>clipboard示例</title> <script src="clipboard.min.js"></script> </head> <body> <hr> <h2>从另外一个元素复制内容</h2> <p> <span id="bar">5670</span> <button id="btn2" data-clipboard-target="#bar">复制到剪切板</button> </p> <hr> <script> //从另外一个元素复制内容 var btn2 = document.getElementById('btn2'); var clipboard2 = new ClipboardJS(btn2);//实例化 clipboard2.on('success', function(e) {//复制成功执行的回调,可选 alert("复制成功!"); console.log(e); }); clipboard2.on('error', function(e) {//复制失败执行的回调,可选 console.log(e); }); </script> </body> </html>