午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Javascript 實(shí)現(xiàn)復(fù)制(Copy)動(dòng)作方法大全
    來(lái)源:易賢網(wǎng) 閱讀:2026 次 日期:2014-07-16 18:51:20
    溫馨提示:易賢網(wǎng)小編為您整理了“Javascript 實(shí)現(xiàn)復(fù)制(Copy)動(dòng)作方法大全”,方便廣大網(wǎng)友查閱!

    現(xiàn)在瀏覽器種類也越來(lái)越多,諸如 IE、Firefox、Chrome、Safari等等,因此現(xiàn)在要實(shí)現(xiàn)一個(gè)js復(fù)制內(nèi)容到剪貼板的小功能就不是一件那么容易的事了。

    一、實(shí)現(xiàn)點(diǎn)擊按鈕,復(fù)制文本框中的的內(nèi)容

    <script type="text/javascript">

    function copyUrl2()

    {

    var Url2=document.getElementById("biao1");

    Url2.select(); // 選擇對(duì)象

    document.execCommand("Copy"); // 執(zhí)行瀏覽器復(fù)制命令

    alert("已復(fù)制好,可貼粘。");

    }

    </script>

    <textarea cols="20" rows="10" id="biao1">用戶定義的代碼區(qū)域</textarea>

    <input type="button" onClick="copyUrl2()" value="點(diǎn)擊復(fù)制代碼" />二、復(fù)制專題地址和 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復(fù)制代碼</title>

    </head>

    <body>

    <p>

    <input type="button" name="anniu1" onClick='copyToClipBoard()' value="復(fù)制專題地址和url地址,傳給QQ/MSN上的好友">

    <script language="javascript">

    function copyToClipBoard(){

    var clipBoardContent="";

    clipBoardContent+=document.title;

    clipBoardContent+="";

    clipBoardContent+=this.location.href;

    window.clipboardData.setData("Text",clipBoardContent);

    alert("復(fù)制成功,請(qǐng)粘貼到你的QQ/MSN上推薦給你的好友");

    }

    </script>三、直接復(fù)制 url

    <input type="button" name="anniu2" onClick='copyUrl()' value="復(fù)制URL地址">

    <script language="javascript">

    function copyUrl()

    {

    var clipBoardContent=this.location.href;

    window.clipboardData.setData("Text",clipBoardContent);

    alert("復(fù)制成功!");

    }

    </script>四、點(diǎn)擊文本框時(shí),復(fù)制文本框里面的內(nèi)容

    <input onclick="oCopy(this)" value="你好.要copy的內(nèi)容!">

    <script language="javascript">

    function oCopy(obj){

    obj.select();

    js=obj.createTextRange();

    js.execCommand("Copy")

    alert("復(fù)制成功!");

    }

    </script>五、復(fù)制文本框或者隱藏域中的內(nèi)容

    <script language="javascript">

    function CopyUrl(target){

    target.value=myimg.value;

    target.select();

    js=myimg.createTextRange();

    js.execCommand("Copy");

    alert("復(fù)制成功!");

    }

    function AddImg(target){

    target.value="[IMG]"+myimg.value+"[/ img]";

    target.select();

    js=target.createTextRange();

    js.execCommand("Copy");

    alert("復(fù)制成功!");

    }

    </script>六、復(fù)制 span 標(biāo)記中的內(nèi)容

    <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("復(fù)制成功!");

    }

    </script>七、瀏覽器兼容  copyToClipboard("拷貝內(nèi)容")

    function copyToClipboard(txt) {

          if (window.clipboardData) {

            window.clipboardData.clearData();

            clipboardData.setData("Text", txt);

            alert("復(fù)制成功!");

          } else if (navigator.userAgent.indexOf("Opera") != -1) {

            window.location = txt;

          } else if (window.netscape) {

            try {

              netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

            } catch (e) {

              alert("被瀏覽器拒絕!\n請(qǐng)?jiān)跒g覽器地址欄輸入'about:config'并回車\n然后將 'signed.applets.codebase_principal_support'設(shè)置為'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("復(fù)制成功!");

          }

        }

    八、兼容各大瀏覽器的復(fù)制代碼(結(jié)合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("該地址已經(jīng)復(fù)制,你可以使用Ctrl+V 粘貼。");

         });

         clip.glue('clip_button', 'clip_container' );

     }

    </script>

    </head>

    <body onLoad="init()">

    <input id="fe_text" cols=50 rows=5 value=復(fù)制內(nèi)容文本1 >

    <span id="clip_container"><span id="clip_button"><b>復(fù)制</b></span></span>

    </body>

    </html>

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)