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)文章
JavaScript實(shí)現(xiàn)簡單的四則運(yùn)算計(jì)算器完整實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡單的四則運(yùn)算計(jì)算器,結(jié)合完整實(shí)例形式分析了javascript基于表單相應(yīng)實(shí)現(xiàn)加減乘除數(shù)學(xué)運(yùn)算的操作技巧,需要的朋友可以參考下2017-04-04
使用formatter函數(shù)設(shè)置Echarts的tooltip中動態(tài)單位
這篇文章主要為大家介紹了使用formatter函數(shù)設(shè)置Echarts的tooltip中動態(tài)單位實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
JS實(shí)現(xiàn)Enter鍵跳轉(zhuǎn)及控件獲得焦點(diǎn)
想讓Enter鍵跳轉(zhuǎn)的同時讓控件獲得焦點(diǎn),具體實(shí)現(xiàn)js代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助2013-08-08
JS增加行復(fù)制行刪除行的實(shí)現(xiàn)代碼
這篇文章介紹了JS增加行復(fù)制行刪除行的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-11-11
js 右側(cè)浮動層效果實(shí)現(xiàn)代碼(跟隨滾動)
因?yàn)轫?xiàng)目上有這樣的需求,在網(wǎng)上也查了些東西,之前是想找個差不多類似的套用一下。后來發(fā)覺沒有合適的,因時間緊迫就自己動手寫了一個簡單的 ,示例代碼如下 兼容火狐和IE7+2015-11-11
Javascript Web Slider 焦點(diǎn)圖示例源碼
Slider 焦點(diǎn)圖會在很多的網(wǎng)站上見到,在本文為大家詳細(xì)介紹下具體的實(shí)現(xiàn)過程,下面的源碼大家可以運(yùn)行下2013-10-10

