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>