复制文本框内容至剪贴板

1 <body>
2     <form id="form1" runat="server">
3     <div>
4         <textarea id="txtArea" cols="30" rows="3">我是一个文本,Hello World!</textarea><br />
5         <input type="button" name="name" value="复制内容" onclick="copyToClipBoard('txtArea')" />
6     </div>
7     </form>
8 </body>

JS:

 1 <script type="text/javascript">
 2         //复制文本框
 3         function copyToClipBoard(id) {
 4             var code = $("#" + id).val();
 5             code = HTMLDeCode(code);
 6             var tip = copyCode(code);
 7             if (tip == 0) {//0 成功 1 失败 2 不支持 3 已经提示
 8                 var _open = tool.getCookieValue("open");
 9                 if (_open == "0" && _global.security == "1") {
10                     alert("您当前的安全级别为高,此时“完全公开”代码将无效\n若要继续选择“完全公开”代码,请将安全级设置为“中”。");
11                 } else {
12                     tool.setCenter($("#box2"), 0);
13                     $("#box2").fadeIn();
14                 }
15             } else if (tip == 1) {
16                 alert("复制失败,请重试!");
17             } else if (tip == 2) {
18                 alert("您的浏览器不支持此功能");
19             }
20             if (typeof pgvSendClick == "function") {
21                 if (tool.getCookieValue("open") == 1) {
22                     pgvSendClick({ hottag: 'R1JT.wp.index.security' });
23                 } else {
24                     pgvSendClick({ hottag: 'R1JT.wp.index.open' });
25                 }
26             }
27 
28         }
29         //去掉特殊符号
30         function HTMLDeCode(str) {
31             var s = "";
32             if (str.length == 0) return "";
33             s = str.replace(/&amp;/g, "&")
34                     .replace(/&lt;/g, "<")
35                     .replace(/&gt;/g, ">")
36                     .replace(/&nbsp;/g, " ")
37                     .replace(/&apos;/g, "'")
38                     .replace(/&quot;/g, "\"");
39             return s;
40         }
41         //执行复制
42         function copyCode(txt) {//0 成功 1 失败 2 不支持 3 已经提示
43             if (window.clipboardData) {//IE
44                 window.clipboardData.clearData();
45                 window.clipboardData.setData("Text", txt);
46                 return 0;
47             } else if (navigator.userAgent.indexOf("Opera") != -1) {//Opera                
48                 return 2;
49             } else if (window.netscape) {//火狐                
50                 try {
51                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
52                 } catch (e) {
53                     alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
54                     return 3;
55                 }
56                 var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
57                 if (!clip)
58                     return 1;
59                 var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
60                 if (!trans)
61                     return 1;
62                 trans.addDataFlavor('text/unicode');
63                 var str = new Object();
64                 var len = new Object();
65                 var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
66                 var copytext = txt;
67                 str.data = copytext;
68                 trans.setTransferData("text/unicode", str, copytext.length * 2);
69                 var clipid = Components.interfaces.nsIClipboard;
70                 if (!clip)
71                     return 1;
72                 clip.setData(trans, null, clipid.kGlobalClipboard);
73                 return 0;
74             } else {//chrome ,safra        
75                 return 2;
76             }
77         } 
78     </script>

IE 能正常复制,火狐 通过设置可以复制

谷歌,Opera 不支持,继续寻找好的解决办法!

posted @ 2012-08-10 11:09  布洛克菲勒  阅读(432)  评论(0编辑  收藏  举报