方法1: 這個方法顯示的效果就是一個按鈕,比較簡單方便,baidu傳情使用的就是這個方法。
<SCRIPT language=JavaScript> function copy(ob){ var obj=findObj(ob); if (obj) { obj.select(); js=obj.createTextRange(); js.execCommand("Copy"); alert("復制成功,您可以粘貼(Ctrl+V)到QQ或MSN上推薦給好友。") } } function findObj(n, d) { document.getElementById('txtUrl').value= document.location.href; var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document); if(!x && document.getElementById) x=document.getElementById(n); return x; } //--> document.write("<input type=\"button\" name=\"Button\" value=\"給朋友看看這篇文章\" onclick=\"copy(\'txtUrl\')\" style=\"cursor:pointer!important;cursor:hand;display:none!important;display:block\" title=\"點擊復制此文章地址到剪貼板中,直接在QQ或MSN上粘貼(Ctrl+V)即可。\">"); document.write("<input id=\"txtUrl\" type=\"hidden\" value="); document.write(self.location+">"); </SCRIPT>
方法1示例:
方法2: 這個方法就是本站使用的方法。
<SCRIPT language=JavaScript> document.write("<br><br>雙擊復制此文章網址,發送給您的朋友:") function copyCode(o){o.select();var js=o.createTextRange();js.execCommand("Copy");alert("復制成功!");} document.write("<textarea onfocus=this.select() style='width:50%;overflow-y:visible;' ondblclick=copyCode(this) rows=1>"); document.write(self.location+"</textarea>"); </SCRIPT>