<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <a id="weixin" onClick="copyFn()" href="javascript:;"> <font color="#FF00FF" id="copyMy">微信号:shaonv77882</font> </a> <span id="copyMy"> 复制我试试</span> <button onClick="copyFn()">点击复制</button> <script> function copyFn(){ var val = document.getElementById('copyMy'); window.getSelection().selectAllChildren(val); document.execCommand ("Copy"); alert("您已经复制微信号!"); } </script> // 注意要复制的标签不能隐藏 //方法二: <script type="text/javascript"> function copyUrl2(){ var Url2=document.getElementById("biao1"); Url2.select(); // 选择对象 document.execCommand("Copy"); // 执行浏览器复制命令 alert("已复制好,可贴粘。"); } </script> <textarea cols="20" rows="10" id="biao1">用户定义的代码区域</textarea> <input type="button" onClick="copyUrl2()" value="点击复制代码" /> </body> </html>