copy到粘贴板

直接上代码,实现了IE和火狐下的copy content


<html>
<head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
String.prototype.replaceAll=function(s1,s2){
return this.replace(new RegExp(s1,"g"),s2);
}

function copy(){
var text = $("#text").html();
CopyContent(text);

}

function CopyContent(copy) {
if (window.clipboardData) {
window.clipboardData.setData("Text", copy);
}
else if (window.netscape) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
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=copy;
alert(copy);
copytext = copytext.replaceAll("\n","\r\n"); //火狐是linux下的换行符,转成windows下的
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);
}

return false;
}

</script>
</head>
<body >


<pre>
<span id='text'>2012/05/21 17:13 GMT+12:00 - receptionist@tp-dco.ntt.com.hk

Sign out time: 2012-05-21 17:13 GMT+12:00, badge returned.
Visitor's Name: 22
Badge number:

Sign out time: 2012-05-21 17:13 GMT+12:00, badge returned.
Visitor's Name: 33
Badge number:

On-loan item: 2012-05-21 17:12 GMT+12:00 Notebook charger 1 pcs nn 2012-05-21 17:13 GMT+12:00 Returned</span>

<input type="button" value='copy' onclick='copy();' />
</pre>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值