js替代copy(示例代碼)
<script type="text/javascript">
function copyCode(id){
var testCode=document.getElementById(id).value;
if(copy2Clipboard(testCode)!=false){
alert("生成的代碼已經(jīng)復(fù)制到粘貼板,你可以使用Ctrl+V 貼到需要的地方去了哦! ");
}
}
copy2Clipboard=function(txt){
if(window.clipboardData){
window.clipboardData.clearData();
window.clipboardData.setData("Text",txt);
}
else if(navigator.userAgent.indexOf("Opera")!=-1){
window.location=txt;
}
else if(window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch(e){
alert("您的firefox安全限制限制您進(jìn)行剪貼板操作,請打開'about:config'將
signed.applets.codebase_principal_support'設(shè)置為true'之后重試,相對路徑為firefox根目
錄/greprefs/all.js");
return false;
}
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=txt;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);
}
}
</script>
<div>
[<input name="" type="button" value="復(fù)制" onclick="copyCode('txtTestCode1')" />]
<br>
<textarea rows=2 id="txtTestCode1" style="width:500px;font-family: Verdana, Arial;font-
size:12px;height:55px;border:1px solid #999999;">復(fù)制內(nèi)容1</textarea>
</div>
相關(guān)文章
利用canvas實(shí)現(xiàn)的加載動畫效果實(shí)例代碼
之前看到一個(gè)Android的加載效果不錯,一直想自己動手做一個(gè),正好這段時(shí)間重溫了一個(gè)Canvas,所以就嘗試了一下。下面這篇文章主要給大家介紹了關(guān)于利用canvas實(shí)現(xiàn)加載效果的相關(guān)資料,需要的朋友可以參考下。2017-07-07js實(shí)現(xiàn)根據(jù)文件url批量壓縮下載成zip包
本文主要介紹了js實(shí)現(xiàn)根據(jù)文件url批量壓縮下載成zip包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02javascript DOM操作之動態(tài)刪除TABLE多行
DOM動態(tài)刪除TABLE tr行的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-12-12JS實(shí)現(xiàn)OCX控件的事件響應(yīng)示例
JS支持OCX控件的事件(event),當(dāng)OCX控件定義的事件發(fā)生時(shí),JS可以捕獲該事件并對事件進(jìn)行相應(yīng)的處理2014-09-09