<div class="codetitle"> <span><a style="CURSOR: pointer" data="57136" class="copybut" id="copybut57136" onclick="doCopy('code57136')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code57136"> <br> <br> <br><title>支持ie与FireFox的剪切板代码</title> <br><script type="text/javascript"> <BR>function setCopy(_sTxt){ <BR>try{ <BR>if(window.clipboardData){ <BR>window.clipboardData.setData("Text", _sTxt); <BR>} else if(window.netscape) {//FireFox搞得真复杂呀 <BR>netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); <BR>var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); <BR>if(!clip) return; <BR>var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); <BR>if(!trans) 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 = _sTxt; <BR>str.data = copytext; <BR>trans.setTransferData("text/unicode", str, copytext.length*2); <BR>var clipid = Components.interfaces.nsIClipboard; <BR>if (!clip) return false; <BR>clip.setData(trans, null, clipid.kGlobalClipboard); <BR>} <BR>}catch(e){} <BR>} <BR></script> <br> <br> <br><input type="button" onclick="setCopy('hello');" value="复制"> <br><div> <br>Firefox/3.0.14下可以运行<br> <br>Firefox/3.5.3下无效<br> <br>不过多考虑这一块了。 <br> </div> <br> <br><br> </div> <br><br>下面是ie firefox 读取剪切板中的信息的方法<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="48850" class="copybut" id="copybut48850" onclick="doCopy('code48850')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code48850"> <br>function getClipboard() <br>{ <br>if (window.clipboardData) <br>{ <br>return (window.clipboardData.getData('text')); <br>} <br>else <br>{ <br>if (window.netscape) <br>{ <br>try <br>{ <br>netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); <br>var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard); <br>if (!clip) <br>{ <br>return; <br>} <br>var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable); <br>if (!trans) <br>{ <br>return; <br>} <br>trans.addDataFlavor("text/unicode"); <br>clip.getData(trans, clip.kGlobalClipboard); <br>var str = new Object(); <br>var len = new Object(); <br>trans.getTransferData("text/unicode", str, len); <br>} <br>catch (e) <br>{ <br>alert("您的firefox安全限制限制您进行剪贴板操作,请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试,相对路径为firefox根目录/greprefs/all.js"); <br>return null; <br>} <br>if (str) <br>{ <br>if (Components.interfaces.nsISupportsWString) <br>{ <br>str = str.value.QueryInterface(Components.interfaces.nsISupportsWString); <br>} <br>else <br>{ <br>if (Components.interfaces.nsISupportsString) <br>{ <br>str = str.value.QueryInterface(Components.interfaces.nsISupportsString); <br>} <br>else <br>{ <br>str = null; <br>} <br>} <br>} <br>if (str) <br>{ <br>return (str.data.substring(0, len.value / 2)); <br>} <br>} <br>} <br>return null; <br>} <br> </div> <br>下面是 ie,firefox下 写入剪切板的代码<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="26823" class="copybut" id="copybut26823" onclick="doCopy('code26823')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code26823"> <br>copy2Clipboard=function(txt){ <br> if(window.clipboardData){ <br>window.clipboardData.clearData(); <br> window.clipboardData.setData("Text",txt); <br> } <br> else if(navigator.userAgent.indexOf("Opera")!=-1){ <br> window.location=txt; <br> } <br> else if(window.netscape){ <br> try{ <br> netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); <br> } <br> catch(e){ <br> alert("您的firefox安全限制限制您进行剪贴板操作,请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试,相对路径为firefox根目录/greprefs/all.js"); <br> return false; <br> } <br> var clip=Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); <br> if(!clip)return; <br> var trans=Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); <br> if(!trans)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;str.data=copytext; <br> trans.setTransferData("text/unicode",str,copytext.length*2); <br> var clipid=Components.interfaces.nsIClipboard; <br> if(!clip)return false; <br> clip.setData(trans,null,clipid.kGlobalClipboard); <br> } <br>}<br> </div> <br>具体的应用,可以参考脚本之家的代码。