这是模仿优酷视频分享的js复制按钮,兼容各种浏览器
<!--复制按钮js--> <script> function copyToClipboard(txt) { if (window.clipboardData) { window.clipboardData.clearData(); window.clipboardData.setData("Text",document.getElementById(txt).value); } else if (navigator.userAgent.indexOf("Opera") != -1) { //do nothing } else if (window.netscape) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'"); } var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return; var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); if (!trans) return; trans.addDataFlavor('text/unicode'); var str = new Object(); var len = new Object(); var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); var copytext = txt; str.data = copytext; trans.setTransferData("text/unicode", str, copytext.length * 2); var clipid = Components.interfaces.nsIClipboard; if (!clip) return false; clip.setData(trans, null, clipid.kGlobalClipboard); } alert("你已经成功复制本地址,请直接粘贴推荐给你的朋友!"); } </script>
*********************************************************************************************
使用:
<!--分享对话框--> <div class="share" > <div class="sharetop"></div> <div class="sharebord"> <h4> 分享给站外好友</h4> <div class="item"><span class="label"> 视频地址 : </span> <input type="text" id="link1" value="http://www.moredoo.com/10002" /><button οnclick="javascript:copyToClipboard('link1');">复制</button></div> <h4> 把视频贴到Blog或BBS</h4> <div class="item"><span class="label"> flash地址: </span> <input type="text" id="link2" value="http://www.moredoo.com/10002" /> <button οnclick="javascript:copyToClipboard('link2');">复制</button></div> <div class="item"><span class="label"> html代码 : </span> <input id="link3" type="text" value='<embed src="http://www.moredoo.com/10002" allowFullScreen="true" quality="high" width="480" height="400" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>' /> <button οnclick="javascript:copyToClipboard('link3');">复制</button></div></div> <div class="sharebutom">
<!--复制按钮js--> <script> function copyToClipboard(txt) { if (window.clipboardData) { window.clipboardData.clearData(); window.clipboardData.setData("Text",document.getElementById(txt).value); } else if (navigator.userAgent.indexOf("Opera") != -1) { //do nothing } else if (window.netscape) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'"); } var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return; var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); if (!trans) return; trans.addDataFlavor('text/unicode'); var str = new Object(); var len = new Object(); var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); var copytext = txt; str.data = copytext; trans.setTransferData("text/unicode", str, copytext.length * 2); var clipid = Components.interfaces.nsIClipboard; if (!clip) return false; clip.setData(trans, null, clipid.kGlobalClipboard); } alert("你已经成功复制本地址,请直接粘贴推荐给你的朋友!"); } </script>
*********************************************************************************************
使用:
<!--分享对话框--> <div class="share" > <div class="sharetop"></div> <div class="sharebord"> <h4> 分享给站外好友</h4> <div class="item"><span class="label"> 视频地址 : </span> <input type="text" id="link1" value="http://www.moredoo.com/10002" /><button οnclick="javascript:copyToClipboard('link1');">复制</button></div> <h4> 把视频贴到Blog或BBS</h4> <div class="item"><span class="label"> flash地址: </span> <input type="text" id="link2" value="http://www.moredoo.com/10002" /> <button οnclick="javascript:copyToClipboard('link2');">复制</button></div> <div class="item"><span class="label"> html代码 : </span> <input id="link3" type="text" value='<embed src="http://www.moredoo.com/10002" allowFullScreen="true" quality="high" width="480" height="400" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>' /> <button οnclick="javascript:copyToClipboard('link3');">复制</button></div></div> <div class="sharebutom">
效果如下
![](http://hi.csdn.net/attachment/201108/30/0_1314665240Hk1Z.gif)