1 <script type="text/javascript"> 2 function copyUrl2() 3 { 4 var Url2=document.getElementById("biao1"); 5 Url2.select(); // 选择对象 6 document.execCommand("Copy"); // 执行浏览器复制命令 7 alert("已复制好,可贴粘。"); 8 } 9 </script> 10 <textarea cols="20" rows="10" id="biao1">用户定义的代码区域</textarea> 11 <input type="button" onClick="copyUrl2()" value="点击复制代码" />
// 注意要复制的标签不能隐藏
方法二:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>test</title> </head> <body> <div> <span id="copyMy"> 复制我试试</span> <button onClick="copyFn()">点击复制</button> </div> <script> function copyFn(){ var val = document.getElementById('copyMy'); window.getSelection().selectAllChildren(val); document.execCommand ("Copy"); } </script> </body> </html>