JS實現各種複製到剪貼簿

來源:互聯網
上載者:User

標籤:border   get   complete   拒絕   bsp   str   sse   命令   one   

一、實現點擊按鈕,複製文字框中的的內容

 
            <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="點擊複製代碼" />

二、複製專題地址和 url 地址,傳給 QQ/MSN 上的好友

 
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Js複製代碼</title></head><body><p><input type="button" name="anniu1" onClick=‘copyToClipBoard()‘ value="複製專題地址和url地址,傳給QQ/MSN上的好友"><script language="javascript">function copyToClipBoard(){var clipBoardContent="";clipBoardContent+=document.title;clipBoardContent+="";clipBoardContent+=this.location.href;window.clipboardData.setData("Text",clipBoardContent);alert("複製成功,請粘貼到你的QQ/MSN上推薦給你的好友");
}
</script>

三、直接複製 url

 
          <input type="button" name="anniu2" onClick=‘copyUrl()‘ value="複製URL地址">
<
script language="javascript">function copyUrl(){var clipBoardContent=this.location.href;window.clipboardData.setData("Text",clipBoardContent);alert("複製成功!");}</script>

四、點擊文字框時,複製文字框裡面的內容
 

          <input onclick="oCopy(this)" value="你好.要copy的內容!"><script language="javascript">function oCopy(obj){
obj.select();
js=obj.createTextRange();js.execCommand("Copy")
alert("複製成功!");
}</script>

五、複製文字框或者隱藏欄位中的內容

 
                 <script language="javascript">function CopyUrl(target){target.value=myimg.value;target.select();js=myimg.createTextRange();js.execCommand("Copy");alert("複製成功!");}function AddImg(target){target.value="[IMG]"+myimg.value+"[/ img]";target.select();js=target.createTextRange();js.execCommand("Copy");alert("複製成功!");}</script>

六、複製 span 標記中的內容

 
                <script type="text/javascript"></script><br /><br /><script type="text/javascript">function copyText(obj){var rng = document.body.createTextRange();rng.moveToElementText(obj);rng.scrollIntoView();rng.select();rng.execCommand("Copy");rng.collapse(false);alert("複製成功!");}</script>

七、瀏覽器安全色  copyToClipboard("拷貝內容")
 

                                   function copyToClipboard(txt) {      if (window.clipboardData) {        window.clipboardData.clearData();        clipboardData.setData("Text", txt);        alert("複製成功!");        else if (navigator.userAgent.indexOf("Opera") != -1) {        window.location = txt;      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("複製成功!");      }    }

八、相容各大瀏覽器的複製代碼(結合ZeroClipboard.js)

 

 

                             <html><head><title>Zero Clipboard Test</title><script type="text/javascript" src="ZeroClipboard.js"></script><script language="JavaScript"> var clip = null; function $(id) { return document.getElementById(id); } function init() {     clip = new ZeroClipboard.Client();     clip.setHandCursor(true);         clip.addEventListener(‘mouseOver‘, function (client) {  // update the text on mouse over  clip.setText( $(‘fe_text‘).value );     });           clip.addEventListener(‘complete‘, function (client, text) {  //debugstr("Copied text to clipboard: " + text );  alert("該地址已經複製,你可以使用Ctrl+V 粘貼。");     });     clip.glue(‘clip_button‘, ‘clip_container‘ ); }</script></head><body onLoad="init()"><input id="fe_text" cols=50 rows=5 value=複製內容文本1 ><span id="clip_container"><span id="clip_button"><b>複製</b></span></span></body></html

JS實現各種複製到剪貼簿

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.