直接上代码,实现了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>