jQuery点击文本框复制其内容到剪贴板上

 1 //点击文本框复制其内容到剪贴板上方法

 2 

 3     function copyToClipboard(txt) {

 4     if (window.clipboardData) {

 5         window.clipboardData.clearData();

 6         window.clipboardData.setData("Text", txt);

 7         alert("已经成功复制到剪帖板上!");

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

 9         window.location = txt;

10     } else if (window.netscape) {

11         try {

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

13         } catch (e) {

14             alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");

15         }

16         var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);

17         if (!clip) return;

18         var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);

19         if (!trans) return;

20         trans.addDataFlavor('text/unicode');

21         var str = new Object();

22         var len = new Object();

23         var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

24         var copytext = txt;

25         str.data = copytext;

26         trans.setTransferData("text/unicode", str, copytext.length * 2);

27         var clipid = Components.interfaces.nsIClipboard;

28         if (!clip) return false;

29         clip.setData(trans, null, clipid.kGlobalClipboard);

30         alert("已经成功复制到剪帖板上!");

31     }

32 }

 

 1 //点击文本框复制其内容到剪贴板上

 2 function setCopyLink() {

 3     $("#txt_CopyLink").val(document.URL)

 4     .focus(function() {

 5         $(this).css({

 6             "background-color": "#ddd"

 7         }).select();

 8         copyToClipboard($("#txt_CopyLink").val());

 9     }).blur(function() {

10         $(this).css({

11             "background-color": "#fff"

12         });

13     });

14 }

 

你可能感兴趣的:(jquery)