Javascript 實現(xiàn)復制(Copy)動作大全

來源 | https://www.liqingbo.cn/blog-1412.html
1、實現(xiàn)點擊按鈕,復制文本框中的的內(nèi)容
<script type="text/javascript">function copyUrl2(){var Url2=document.getElementById("biao1");Url2.select(); // 選擇對象document.execCommand("Copy"); // 執(zhí)行瀏覽器復制命令alert("已復制好,可貼粘。");}script><textarea cols="20" rows="10" id="biao1">用戶定義的代碼區(qū)域textarea><input type="button" onClick="copyUrl2()" value="點擊復制代碼" />
2、復制專題地址和 url 地址,傳給 QQ/MSN 上的好友
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Js復制代碼title>head><body><p><input type="button" name="anniu1" onClick='copyToClipBoard()' value="復制專題地址和url地址,傳給QQ/MSN上的好友"><script language="javascript">function copyToClipBoard(){var clipBoardContent="";clipBoardContent+=document.title;clipBoardContent+="";clipBoardContent+=this.location.href;window.clipboardData.setData("Text",clipBoardContent);alert("復制成功,請粘貼到你的QQ/MSN上推薦給你的好友");}script>
3、直接復制 url
<input type="button" name="anniu2" onClick='copyUrl()' value="復制URL地址"><script language="javascript">function copyUrl(){var clipBoardContent=this.location.href;window.clipboardData.setData("Text",clipBoardContent);alert("復制成功!");}script>
4、點擊文本框時,復制文本框里面的內(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("復制成功!");}script>
5、復制文本框或者隱藏域中的內(nèi)容
<script language="javascript">function CopyUrl(target){target.value=myimg.value;target.select();js=myimg.createTextRange();js.execCommand("Copy");alert("復制成功!");}function AddImg(target){target.value="[IMG]"+myimg.value+"[/ img]";target.select();js=target.createTextRange();js.execCommand("Copy");alert("復制成功!");}script>
6、復制 span 標記中的內(nèi)容
<script type="text/javascript">script><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("復制成功!");}script>
function copyToClipboard(txt) {if (window.clipboardData) {window.clipboardData.clearData();clipboardData.setData("Text", txt);alert("復制成功!");} else if (navigator.userAgent.indexOf("Opera") != -1) {window.location = txt;} else if (window.netscape) {try {netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");} catch (e) {alert("被瀏覽器拒絕!\n請在瀏覽器地址欄輸入'about:config'并回車\n然后將 'signed.applets.codebase_principal_support'設置為'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("復制成功!");}}
8、兼容各大瀏覽器的復制代碼(結合ZeroClipboard.js)
<html><head><title>Zero Clipboard Testtitle><script type="text/javascript" src="ZeroClipboard.js">script><script language="JavaScript">var clip = null;function $(id) { return document.getElementById(id); }function init() {clip = new ZeroClipboard.Client();clip.setHandCursor(true);clip.addEventListener('mouseOver', function (client) {// update the text on mouse overclip.setText( $('fe_text').value );});clip.addEventListener('complete', function (client, text) {//debugstr("Copied text to clipboard: " + text );alert("該地址已經(jīng)復制,你可以使用Ctrl+V 粘貼。");});clip.glue('clip_button', 'clip_container' );}script>head><body onLoad="init()"><input id="fe_text" cols=50 rows=5 value=復制內(nèi)容文本1 ><span id="clip_container"><span id="clip_button"><b>復制b>span>span>body>html>
本文完~

評論
圖片
表情
