js實(shí)現(xiàn)復(fù)制功能(多種方法集合)
1.實(shí)現(xiàn)點(diǎn)擊按鈕,復(fù)制文本框中的的內(nèi)容
<script type="text/javascript">
function copyUrl2()
{
var Url2=document.getElementById("biao1");
Url2.select(); // 選擇對(duì)象
document.execCommand("Copy"); // 執(zhí)行瀏覽器復(fù)制命令
alert("已復(fù)制好,可貼粘。");
}
</script>
<textarea cols="20" rows="10" id="biao1">用戶(hù)定義的代碼區(qū)域</textarea>
<input type="button" onClick="copyUrl2()" value="點(diǎn)擊復(fù)制代碼" />
2.復(fù)制專(zhuān)題地址和 url 地址,傳給 QQ/MSN 上的好友
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Js復(fù)制代碼</title>
</head>
<body>
<p>
<input type="button" name="anniu1" onClick='copyToClipBoard()' value="復(fù)制專(zhuān)題地址和url地址,傳給QQ/MSN上的好友">
<script language="javascript">
function copyToClipBoard(){
var clipBoardContent="";
clipBoardContent+=document.title;
clipBoardContent+="";
clipBoardContent+=this.location.href;
window.clipboardData.setData("Text",clipBoardContent);
alert("復(fù)制成功,請(qǐng)粘貼到你的QQ/MSN上推薦給你的好友");
}
</script>
3.直接復(fù)制 url
<input type="button" name="anniu2" onClick='copyUrl()' value="復(fù)制URL地址">
<script language="javascript">
function copyUrl()
{
var clipBoardContent=this.location.href;
window.clipboardData.setData("Text",clipBoardContent);
alert("復(fù)制成功!");
}
</script>
4.點(diǎn)擊文本框時(shí),復(fù)制文本框里面的內(nèi)容
<input onclick="oCopy(this)" value="你好.要copy的內(nèi)容!">
<script language="javascript">
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert("復(fù)制成功!");
}
</script>
5.復(fù)制文本框或者隱藏域中的內(nèi)容
<script language="javascript">
function CopyUrl(target){
target.value=myimg.value;
target.select();
js=myimg.createTextRange();
js.execCommand("Copy");
alert("復(fù)制成功!");
}
function AddImg(target){
target.value="[IMG]"+myimg.value+"[/ img]";
target.select();
js=target.createTextRange();
js.execCommand("Copy");
alert("復(fù)制成功!");
}
</script>
6.復(fù)制 span 標(biāo)記中的內(nèi)容
<script type="text/javascript">
</script>
<br />
<br />
<script type="text/javascript">function copyText(obj)
{
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
alert("復(fù)制成功!");
}
</script>
7.瀏覽器兼容 copyToClipboard("拷貝內(nèi)容")
function copyToClipboard(txt) {
if (window.clipboardData) {
window.clipboardData.clearData();
clipboardData.setData("Text", txt);
alert("復(fù)制成功!");
} else if (navigator.userAgent.indexOf("Opera") != -1) {
window.location = txt;
} else if (window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("被瀏覽器拒絕!\n請(qǐng)?jiān)跒g覽器地址欄輸入'about:config'并回車(chē)\n然后將 'signed.applets.codebase_principal_support'設(shè)置為'true'");
}
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);
alert("復(fù)制成功!");
}
}
js實(shí)現(xiàn)但擊自動(dòng)選中文本
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>自動(dòng)選擇文本框/編輯框中的文字</title>
<script type="text/javascript">
function Myselect_txt(){
if (document.form1.title.focus){
document.form1.title.select();}
}
function Myselect_txtarea(){
if (document.form1.content.focus){
document.form1.content.select();}
}
</script>
</head>
<body style="font-size:12px">
<table width="443" height="97" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#f9f9f9">
<form name="form1">
<tr bgcolor="#214994">
<td height="25" colspan="2"><div align="center"><font color="#FFFFFF">新聞信息修改</font></div></td>
</tr>
<tr>
<td width="80" height="28"><div align="right"><font color="#214994">新聞標(biāo)題:</font></div></td>
<td width="363"><input name="title" type="text" size="50" value="今日新聞?lì)^條" onClick="Myselect_txt()"></td>
</tr>
<tr>
<td height="20"><div align="right"><font color="#214994">新聞內(nèi)容:</font></div></td>
<td rowspan="2"><textarea name="content" cols="50" rows="6" onClick="Myselect_txtarea()">今日,據(jù)相關(guān)方面報(bào)道,...</textarea></td>
</tr>
<tr>
<td height="10"> </td>
</tr>
<tr>
<td height="32" colspan="2">
<div align="center">
<input name="add" type="submit" id="add" value="添加">
<input name="Submit" type="reset" value="重置">
</div></td>
</tr>
</form>
</table>
</body>
</html>
當(dāng)前最常用的最簡(jiǎn)潔還是這個(gè),代碼少,減少頁(yè)面加載速度
function copyToClipboard(txt) {
if(window.clipboardData){
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
alert('復(fù)制成功!')
}else{
alert('請(qǐng)手動(dòng)復(fù)制!')
}
}
到此介紹這么多了,希望大家多多關(guān)注,腳本之家以前的文章。
- C#后臺(tái)接受前臺(tái)JSON字符串裝換成字典集合處理
- 詳談js遍歷集合(Array,Map,Set)
- jQuery實(shí)現(xiàn)簡(jiǎn)單復(fù)制json對(duì)象和json對(duì)象集合操作示例
- JS中的算法與數(shù)據(jù)結(jié)構(gòu)之集合(Set)實(shí)例詳解
- Redis緩存,泛型集合與json字符串的相互轉(zhuǎn)換實(shí)例
- python將字典內(nèi)容寫(xiě)入json文件的實(shí)例代碼
- python字典與json轉(zhuǎn)換的方法總結(jié)
- python 如何將字典寫(xiě)為json文件
- JavaScript?數(shù)據(jù)結(jié)構(gòu)之字典方法
- JavaScript字典與集合詳解
相關(guān)文章
JS數(shù)組降維的實(shí)現(xiàn)Array.prototype.concat.apply([], arr)
這篇文章主要介紹了JS數(shù)組降維的實(shí)現(xiàn)Array.prototype.concat.apply([], arr),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
查詢(xún)綁定數(shù)據(jù)島的表格中的文本并修改顯示方式的js代碼
查詢(xún)綁定數(shù)據(jù)島的表格中的文本并修改顯示方式的js代碼2009-12-12
javascript 瀏覽器判斷 綁定事件 arguments 轉(zhuǎn)換數(shù)組 數(shù)組遍歷
javascript 瀏覽器 判斷 綁定事件 arguments 轉(zhuǎn)換數(shù)組 數(shù)組遍歷等函數(shù)2009-07-07
微信小程序使用map組件實(shí)現(xiàn)路線(xiàn)規(guī)劃功能示例
這篇文章主要介紹了微信小程序使用map組件實(shí)現(xiàn)路線(xiàn)規(guī)劃功能,結(jié)合具體實(shí)例形式分析了微信小程序基于map組件的地理位置獲取、路徑規(guī)劃等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
javascript中的return和閉包函數(shù)淺析
這篇文章主要介紹了javascript中的return和閉包函數(shù)淺析,至少可以讓你搞懂那么多()是什么意思,需要的朋友可以參考下2014-06-06
解決layui前端框架 form表單,table表等內(nèi)置控件不顯示的問(wèn)題
今天小編就為大家分享一篇解決layui前端框架 form表單,table表等內(nèi)置控件不顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

