欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能兼容所有瀏覽器(推薦)

 更新時(shí)間:2023年05月02日 16:32:45   作者:白樹(shù)  
這篇文章主要介紹了JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能兼容所有瀏覽器(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

兩天前聽(tīng)了一個(gè)H5的分享,會(huì)議上有一句話,非常有感觸:不是你不能,而是你對(duì)自己的要求太低。很簡(jiǎn)單的一句話,相信很多事情不是大家做不到,真的是對(duì)自己的要求太低,如果對(duì)自己要求多一點(diǎn),那么你取得的進(jìn)步可能會(huì)更大。成長(zhǎng)以來(lái),很多朋友也聽(tīng)說(shuō)到不少激勵(lì)自己上進(jìn)的話,但不是每個(gè)人都能一直堅(jiān)持做下來(lái),其實(shí),這個(gè)跟自己的性格以及周圍的環(huán)境都有很大關(guān)系,只能說(shuō)多找方法、條件給自己鼓勵(lì),不斷提高對(duì)自己的要求,才有機(jī)會(huì)獲得多一點(diǎn)的成就。

2023年最好用的復(fù)制代碼

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    </head>
    <body>
        <span class="orange" id="shareUrl" data-url="www.baidu.com">www.baidu.com</span>
        <script>
            // 復(fù)制
            function copyText(text) {
                var textArea = document.createElement("textarea");
                // textArea.style['display']='none'
                textArea.style['position'] = 'absolute'
                textArea.style['top'] = '0'
                textArea.style['left'] = '0'
                textArea.value = text;
                document.body.appendChild(textArea);
                textArea.focus();
                textArea.select();
                try {
                    var successful = document.execCommand('copy');
                    var msg = successful ? 'successful' : 'unsuccessful';
                    console.log(msg)
                } catch (err) {
                    console.error('復(fù)制失敗', err);
                }
                document.body.removeChild(textArea);
                if (successful) {
                    return true
                }
            };
            $(function() {
                var shareUrl = $("#shareUrl").data("url");
                $("#shareUrl").click(function() {
                    var shareUrl = $("#shareUrl").data("url");
                    if (copyText(shareUrl)) {
                        alert("復(fù)制成功");
                    }
                })
            })
        </script>
    </body>
</html>

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

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

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

相信很多使用wordpress搭建過(guò)站點(diǎn)的同學(xué)都知道它采用了jQuery,對(duì)jQuery大家并不陌生,使用起來(lái)非常簡(jiǎn)單,可惜jQuery本身并沒(méi)有實(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原來(lái)是有個(gè)父親大人,叫Zero Clipboard。

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

現(xiàn)在我們先使用獨(dú)立的js庫(kù)Zero Clipboard簡(jiǎn)單實(shí)現(xiàn)復(fù)制到剪貼板功能,demo如下:

<!DOCTYPE html>
<html>
<head>
<title>Zero Clipboard Test</title>
<meta charset="utf-8">
</head>
<body>
<!-- 

說(shuō)明:

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> 

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

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

接下來(lái)介紹jQuery ZeroClipboard

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如下:

<!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>

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

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

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

以上所述是小編給大家介紹的JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能兼容所有瀏覽器(推薦),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論