溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

js實(shí)現(xiàn)復(fù)制功能(多種方法集合)

發(fā)布時(shí)間:2020-09-11 21:37:43 來源:腳本之家 閱讀:165 作者:mdxy-dxy 欄目:web開發(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">用戶定義的代碼區(qū)域</textarea>
<input type="button" onClick="copyUrl2()" value="點(diǎn)擊復(fù)制代碼" />

2.復(fù)制專題地址和 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ù)制專題地址和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'并回車\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 >
<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">&nbsp;</td>
  </tr>
  <tr>
   <td height="32" colspan="2">
    <div align="center">
     <input name="add" type="submit" id="add" value="添加">
&nbsp;
     <input name="Submit" type="reset" value="重置">
   </div></td>
  </tr>
 </form>
</table>
</body>
</html>

當(dāng)前最常用的最簡(jiǎn)潔還是這個(gè),代碼少,減少頁面加載速度

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)注,億速云以前的文章。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI