复制代码 代码如下: CSS/Javascript demo <BR> function test(){<BR> copyToClipboard("dddd");<BR> }<BR> copyToClipboard = function(txt) {<BR>if(window.clipboardData) {<BR> window.clipboardData.clearData();<BR> window.clipboardData.setData("Text", txt);<BR>} else if(navigator.userAgent.indexOf("Opera") != -1) {<BR>window.location = txt;<BR>} else if (window.netscape) {<BR>try {<BR>netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");<BR>} catch (e) {<BR>alert("您的firefox安全限制限制您进行剪贴板操作,请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试");<BR>return false;<BR>}<BR>var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);<BR>if (!clip)<BR>return;<BR>var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);<BR>if (!trans)<BR>return;<BR>trans.addDataFlavor('text/unicode');<BR>var str = new Object();<BR>var len = new Object();<BR>var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);<BR>var copytext = txt;<BR>str.data = copytext;<BR>trans.setTransferData("text/unicode",str,copytext.length*2);<BR>var clipid = Components.interfaces.nsIClipboard;<BR>if (!clip)<BR>return false;<BR>clip.setData(trans,null,clipid.kGlobalClipboard);<BR>}<BR>}<BR> 复制到粘贴板 说明:Firefox的默认安全限制不允许通过JavaScript复制到剪贴板,需要提示用户修改Firefox的设置。修改方法是打开"about:config"将"signed.applets.codebase_principal_support"设置为"true"。