標籤:des style class blog code java
相信這個功能大家平時上網經常能碰到,以前也沒怎麼留意怎麼實現的,直到項目中需要。
網上一搜一大堆,單純使用js方法也不是沒有,但是由於各瀏覽器的安全機制不同,不是跨瀏覽器的。去看了幾個常用的網站,都是用的透明flash遮擋“複製到剪貼簿”按鈕,所以當你點擊“複製到剪貼簿”的時候,點擊的其實是flash,然後把你需要複製的內容傳入到了flash,然後在通過flash的複製功能把傳入的內容複寫到了剪貼簿。
1. 準備工作
準備好jquery,然後去http://www.steamdev.com/zclip/這裡下載jquery.zclip.js和ZeroClipboard.swf ,不過那裡的ZeroClipboard.swf失效了,你可以去這裡http://pan.baidu.com/share/link?shareid=1286340661&uk=2133883598&fid=2382679931下載,我測試過可以使用。
在html裡加上
<script type="text/javascript" src="jquery.min.js"></script><script type="text/javascript" src="jquery.zclip.js"></script>
2. 編寫代碼
下面是一個小demo,主要是複製文字框中的連結到剪貼簿。
<input type="text" value="www.baidu.com" id="link"><span id="copyBtn">複製連結</span>
然後加入指令碼,非常的簡單。
<script> $(‘#copyBtn‘).zclip({ path: "ZeroClipboard.swf", copy: function(){ return $(‘#link‘).val(); } });</script>
這裡的path是你flash的路徑,copy是複製成功之後的回呼函數。返回的是文字框裡的value值。
另外我們還要對jquery.zclip.js進行設定,用編輯器開啟它,就在前面,你會看到如下代碼:
var settings = $.extend({ path: ‘ZeroClipboard.swf‘, copy: null, beforeCopy: null, afterCopy: null, clickAfter: true, setHandCursor: true, setCSSEffects: true }, params);
同樣修改path為你的flash--ZeroClipboard.swf的路徑,其他的設定我們先不管他。
好了,立馬來測試吧,不過這裡要注意的是,本地測試是不成功的,你要用tomcat或apache在本機搭一個伺服器,然後把檔案放到伺服器目錄下測試。你會看到移到複製連結上的時候出現了小手,又見是flash的菜單,:
然後點擊它,出現成功提示框。你可以試著在其他地方粘貼,看看有沒有複製成功。
當然你也可以修改複製成功之後的提示框,或者添加更多功能。
在中找到如下代碼。
clip.addEventListener(‘complete‘, function(client, text) { if ($.isFunction(settings.afterCopy)) { o.trigger(‘zClip_afterCopy‘); } else { if (text.length > 500) { text = text.substr(0, 500) + "...\n\n(" + (text.length - 500) + " characters not shown)"; } o.removeClass(‘hover‘); alert("Copied text to clipboard:\n\n " + text); } if (settings.clickAfter) { o.trigger(‘click‘); } });
修改alert那個地方的代碼就可以了,也許還有其他地方也可以修改,我沒有細看。
3. 其他
我們用chrome開啟我們的demo,開啟控制台可以看到
沒錯,這正是外掛程式給我們的頁面插入了flash,可以看到它正好蓋住了我們的”複製連結“按鈕。
好了,使用外掛程式zClip來實現複製到剪貼簿的功能就介紹到這裡了。更多的細節請訪問:http://www.steamdev.com/zclip/
詳細介紹了外掛程式的用法,以及相關參數的設定,包括複製成功之後的函數,怎麼給”複製連結“按鈕添加樣式等等。
Usage
1.) Add jQuery and zClip to your document.
<script type="text/javascript" src="js/jquery.js"></script><script type="text/javascript" src="js/jquery.zclip.js"></script>
2.) Inside of a <script> block, attach zClip to the element which will become your "copy button".
$(document).ready(function(){ $(‘a#copy-description‘).zclip({ path:‘js/ZeroClipboard.swf‘, copy:$(‘p#description‘).text() }); // The link with ID "copy-description" will copy // the text of the paragraph with ID "description" $(‘a#copy-dynamic‘).zclip({ path:‘js/ZeroClipboard.swf‘, copy:function(){return $(‘input#dynamic‘).val();} }); // The link with ID "copy-dynamic" will copy the current value // of a dynamically changing input with the ID "dynamic"});
3.) Supply custom callback functions.
$(document).ready(function(){ $("a#copy-callbacks").zclip({ path:‘js/ZeroClipboard.swf‘, copy:$(‘#callback-paragraph‘).text(), beforeCopy:function(){ $(‘#callback-paragraph‘).css(‘background‘,‘yellow‘); $(this).css(‘color‘,‘orange‘); }, afterCopy:function(){ $(‘#callback-paragraph‘).css(‘background‘,‘green‘); $(this).css(‘color‘,‘purple‘); $(this).next(‘.check‘).show(); } });});
4.) Parameters & Defaults
| Settings |
| Variable |
Default Value |
Available Values |
| path * |
"ZeroClipboard.swf" |
"path/to/ZeroClipboard.swf" |
| copy * |
null |
any string, or any javascript expression or function that returns a string |
| afterCopy |
null |
specify a function to call after text is copied.
(your afterCopy function will overwrite the default alert box.) |
| beforeCopy |
null |
specify a function to call before text is copied. |
| clickAfter |
true |
true
false |
| setHandCursor |
true |
true
false |
| setCSSEffects |
true |
true
false |
| * required |
Notes
1.) Tested compatible in IE 6, IE 7, IE 8, FF 3.6, Chrome 8, Safari 5, Opera 11
2.) For proper CSS effects:
/* zClip is a flash overlay, so it must provide *//* the target element with "hover" and "active" classes *//* to simulate native :hover and :active states. *//* Be sure to write your CSS as follows for best results: */a:hover, a.hover {...}a:active, a.active {...}
3.) Show / Hide / Remove zClip
$(‘a.copy‘).zclip(‘show‘); // enable zClip on the selected element$(‘a.copy‘).zclip(‘hide‘); // hide zClip on the selected element$(‘a.copy‘).zclip(‘remove‘); // remove zClip from the selected element
4.) For best results, attach zClip to your elements once the page has its final layout. There is a function that automatically repositions each flash overlay on window load and resize, but it may not be 100% reliable in every instance.
參考資料:http://www.steamdev.com/zclip/