五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > js/javascript/jQuery實(shí)現(xiàn)復(fù)制到剪貼板的方法(兼容所有瀏覽器)

js/javascript/jQuery實(shí)現(xiàn)復(fù)制到剪貼板的方法(兼容所有瀏覽器)

文章來源:365jz.com     點(diǎn)擊數(shù):2321    更新時(shí)間:2017-12-12 10:48   參與評(píng)論

一、實(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

 

實(shí)現(xiàn)復(fù)制到剪貼板功能,兼容所有瀏覽器:

目錄(支持移動(dòng)端ios復(fù)制

前言

今年下半年打算在組內(nèi)建個(gè)叫『移動(dòng)開發(fā)指南』的站點(diǎn),在網(wǎng)站框架搭建過程,有一個(gè)功能需要實(shí)現(xiàn)復(fù)制文本到剪貼板,相信這個(gè)功能很常用,但是對(duì)于不常寫JS代碼的我來說是一個(gè)比較大的挑戰(zhàn),回想以前做過的一個(gè)站點(diǎn),使用window.clipboardData實(shí)現(xiàn)復(fù)制到剪貼板功能,也僅僅支持IE和FF瀏覽器,當(dāng)時(shí)在百度找個(gè)幾個(gè)方案,看不下去就放棄了,后來在代碼中做了判斷,如果不支持該屬性,就直接alert:此功能不支持該瀏覽器,請(qǐng)手工復(fù)制文本框中內(nèi)容,現(xiàn)在想想真是偷懶啊,嘿嘿,有沒有人中槍啊~

alert("此功能不支持該瀏覽器,請(qǐng)手工復(fù)制文本框中內(nèi)容");

現(xiàn)在網(wǎng)絡(luò)上其實(shí)并沒有比較詳細(xì)講解js實(shí)現(xiàn)復(fù)制到剪貼板功能,很多文章是千遍一律,對(duì)于需要使用復(fù)制到剪貼板功能的童鞋來說,是比較蛋疼的,今天給大家?guī)磉@塊的分享,由于能力有限,有錯(cuò)誤的地方還請(qǐng)大家多多指教~

獨(dú)立的js庫Zero Clipboard簡(jiǎn)單實(shí)現(xiàn)復(fù)制到剪貼板功能

相信很多使用wordpress搭建過站點(diǎn)的同學(xué)都知道它采用了jQuery,對(duì)jQuery大家并不陌生,使用起來非常簡(jiǎn)單,可惜jQuery本身并沒有實(shí)現(xiàn)復(fù)制到剪貼板的功能,但或許它的API會(huì)有這個(gè)功能。這次我搭建的站點(diǎn)采用wordpress,花了點(diǎn)時(shí)間搜索jQuery復(fù)制到剪貼板的API,還真的有:jQuery ZeroClipboard ,于是使用它在wordpress簡(jiǎn)單實(shí)現(xiàn)了復(fù)制到剪貼板的功能。但是呢,jQuery ZeroClipboard原來是有個(gè)父親大人,叫Zero Clipboard。

Zero Clipboard作為一個(gè)獨(dú)立的js庫,它利用 Flash 進(jìn)行復(fù)制,需要兩個(gè)文件:ZeroClipboard.js 和 ZeroClipboard.swf 。網(wǎng)絡(luò)上有2個(gè)版本,實(shí)現(xiàn)原理都是使用flash進(jìn)行復(fù)制,不知道原創(chuàng)是誰的,也可能一家子的2個(gè)兄弟,這個(gè)就不管了,只要我們自己做到尊重版權(quán),表示問心無愧,今天給大家介紹的這個(gè)版本相對(duì)來說比較簡(jiǎn)單。

首先看下圖是為使用Zero Clipboard后生成的flash對(duì)象,它能兼容的flash10及以下版本,兼容所有的瀏覽器:

Zero Clipboard的官方地址:http://zeroclipboard.org/,github地址:https://github.com/zeroclipboard/ZeroClipboard

使用它需要搭建服務(wù)器環(huán)境,可能有同學(xué)不太清楚,關(guān)于搭建服務(wù)器環(huán)境的,方法有很多,如xp或者win7系統(tǒng)自帶的IIS,也可以使用xampp、appserv、APMServ等集成包,安裝即可,搭建起來非常簡(jiǎn)單,這里不做介紹~

demo如下:

復(fù)制代碼
<!DOCTYPE html>
<html>
<head>
<title>Zero Clipboard Test</title>
<meta charset="utf-8">
</head>
<body>
<!-- 
    說明:
    1.data-clipboard-target 輸入要復(fù)制的對(duì)象的ID
-->
<button id="d_clip_button" class="my_clip_button" data-clipboard-target="fe_text"><b>復(fù)制到剪貼板</b></button>
<br/>
<textarea id="fe_text" cols="50" rows="3">輸入需要復(fù)制的內(nèi)容</textarea>
</body>
</html>
<script type="text/javascript" src="ZeroClipboard.js"></script>
<script type="text/javascript">
// 定義一個(gè)新的復(fù)制對(duì)象
var clip = new ZeroClipboard( document.getElementById("d_clip_button"), {
  moviePath: "ZeroClipboard.swf"
} );

// 復(fù)制內(nèi)容到剪貼板成功后的操作
clip.on( 'complete', function(client, args) {
   alert("復(fù)制成功,復(fù)制內(nèi)容為:"+ args.text);
} );

</script>
復(fù)制代碼

demo下載 (溫馨提示:下載代碼的同學(xué),瀏覽demo時(shí)記得使用服務(wù)器環(huán)境,不然看不到效果的~)

上面代碼注釋中已經(jīng)對(duì)Zero Clipboard的功能進(jìn)行了介紹,需要了解更多的功能請(qǐng)到https://github.com/zeroclipboard/ZeroClipboard

 jQuery ZeroClipboard實(shí)現(xiàn)復(fù)制到剪貼板功能

jQuery ZeroClipboard是在ZeroClipboard的基礎(chǔ)上進(jìn)行的改良,簡(jiǎn)稱zClip,作為jQuery的API,jQuery ZeroClipboard也表現(xiàn)的非常簡(jiǎn)易操作,官方地址:http://www.steamdev.com/zclip/

使用前需引用2個(gè)js文件:jquery.js和jquery.zclip.js

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.zclip.js"></script>

現(xiàn)在我們使用jquery.zclip.js簡(jiǎn)單實(shí)現(xiàn)復(fù)制到剪貼板功能demo如下:

復(fù)制代碼
<!DOCTYPE html>
<html>
<head>
<title>ZeroClipboard Test</title>
<meta charset="utf-8">
<style type="text/css">
.line{margin-bottom:20px;}
/* 復(fù)制提示 */
.copy-tips{position:fixed;z-index:999;bottom:50%;left:50%;margin:0 0 -20px -80px;background-color:rgba(0, 0, 0, 0.2);filter:progid:DXImageTransform.Microsoft.Gradient(startColorstr=#30000000, endColorstr=#30000000);padding:6px;}
.copy-tips-wrap{padding:10px 20px;text-align:center;border:1px solid #F4D9A6;background-color:#FFFDEE;font-size:14px;}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.zclip.js"></script>
</head>
<body>
<div class="line">
    <h2>demo1 點(diǎn)擊復(fù)制當(dāng)前文本</h2>
    <a href="#none" class="copy">點(diǎn)擊復(fù)制我</a>
</div>
<div class="line">
    <h2>demo2 點(diǎn)擊復(fù)制表單中的文本</h2>
    <a href="#none" class="copy-input">點(diǎn)擊復(fù)制單中的文本</a>
    <input type="text" class="input" value="輸入要復(fù)制的內(nèi)容" />
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
/* 定義所有class為copy標(biāo)簽,點(diǎn)擊后可復(fù)制被點(diǎn)擊對(duì)象的文本 */
    $(".copy").zclip({
        path: "ZeroClipboard.swf",
        copy: function(){
        return $(this).text();
        },
        beforeCopy:function(){/* 按住鼠標(biāo)時(shí)的操作 */
            $(this).css("color","orange");
        },
        afterCopy:function(){/* 復(fù)制成功后的操作 */
            var $copysuc = $("<div class='copy-tips'><div class='copy-tips-wrap'>? 復(fù)制成功</div></div>");
            $("body").find(".copy-tips").remove().end().append($copysuc);
            $(".copy-tips").fadeOut(3000);
        }
    });
/* 定義所有class為copy-input標(biāo)簽,點(diǎn)擊后可復(fù)制class為input的文本 */
    $(".copy-input").zclip({
        path: "ZeroClipboard.swf",
        copy: function(){
        return $(this).parent().find(".input").val();
        },
        afterCopy:function(){/* 復(fù)制成功后的操作 */
            var $copysuc = $("<div class='copy-tips'><div class='copy-tips-wrap'>? 復(fù)制成功</div></div>");
            $("body").find(".copy-tips").remove().end().append($copysuc);
            $(".copy-tips").fadeOut(3000);
        }
    });
});
</script>
復(fù)制代碼

demo下載 (溫馨提示:下載代碼的同學(xué),瀏覽demo時(shí)記得使用服務(wù)器環(huán)境,不然看不到效果的~)

上面代碼中結(jié)合jQuery的操作節(jié)點(diǎn)的功能,出色的發(fā)揮jquery.zclip.js的作用,如復(fù)制前后的操作,動(dòng)態(tài)插入節(jié)點(diǎn),也可見jquery.zclip.js的強(qiáng)大之處,使用起來是非常簡(jiǎn)單。需要深入了解更多jquery.zclip.js的功能請(qǐng)到http://www.steamdev.com/zclip/

從上面獨(dú)立的js庫ZeroClipboard.js和jquery.zclip.js 都是采用flash實(shí)現(xiàn)實(shí)現(xiàn)復(fù)制到剪貼板的功能,可以看出,使用ZeroClipboard.js帶來功能相對(duì)比較少,不過它是獨(dú)立的庫,文件比較小,而使用jquery.zclip.js后的功能是比較豐富,不過對(duì)于不使用jQuery框架的站點(diǎn)來說,采用jquery.zclip.js是比較浪費(fèi)寬帶。使用哪種復(fù)制方式還是得看產(chǎn)品的具體定位情況~

 

//20170110補(bǔ)充clipboard.js實(shí)現(xiàn)復(fù)制到剪貼板

clipboard.js實(shí)現(xiàn)復(fù)制到剪貼板(含移動(dòng)端ios復(fù)制)

 取代flash復(fù)制到剪切板,更好頁面性能,不會(huì)造成卡頓想象,不止兼容PC端,還優(yōu)雅的兼容移動(dòng)端ios的safari瀏覽器,具體可以參考我做的項(xiàng)目https://wepayui.github.io/#/start/

官方文檔及下載地址:https://clipboardjs.com/

如對(duì)本文有疑問,請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇

發(fā)表評(píng)論 (2321人查看,0條評(píng)論)
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)