JS復(fù)制對應(yīng)id的內(nèi)容到粘貼板(Ctrl+C效果)
前言
最近在做一個(gè)按鈕,實(shí)現(xiàn)的效果是當(dāng)點(diǎn)擊后復(fù)制url到黏貼板,但不是當(dāng)前頁面url,而是對應(yīng)一個(gè)元素的url,且一個(gè)頁面會有多個(gè)url。一開始找到一個(gè)方法,但是竟然只兼容IE瀏覽器,神奇了,竟然有只兼容IE的東西。后來發(fā)現(xiàn)一個(gè)zeroclipboard.js這個(gè)插件,但是怎么也搞不出那個(gè)效果,有點(diǎn)麻煩。
最后翻到了一個(gè)js封裝好的方法,有效!
想要實(shí)現(xiàn)的一個(gè)效果是,下面html代碼:
<tr> <td> <a id="copy_{$key}" onclick="getUrl('{$key}')">復(fù)制文件鏈接</a> <input id="file_{$key}" value="{$file.file_url}" style="margin-left: -9999px"/> </td> </tr>
點(diǎn)擊復(fù)制文件鏈接這個(gè)按鈕,復(fù)制input框里的value值,是傳進(jìn)去的一個(gè)url;首先點(diǎn)擊a標(biāo)簽會觸發(fā)getUrl這個(gè)函數(shù);傳進(jìn)去id用于找到對應(yīng)的input然后取值(因?yàn)楸闅v了多個(gè)td,有許多個(gè)input框一一對應(yīng)去?。?。
下面js代碼:
<pre><script type="application/javascript"> function getUrl(id) { if (copyToClipboard(document.getElementById("file_"+id))){ alert("成功復(fù)制到黏貼板!"); }else{ alert("復(fù)制到黏貼板失??!"); } } function copyToClipboard(elem) { // create hidden text element, if it doesn't already exist var targetId = "_hiddenCopyText_"; var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA"; var origSelectionStart, origSelectionEnd; if (isInput) { // can just use the original source element for the selection and copy target = elem; origSelectionStart = elem.selectionStart; origSelectionEnd = elem.selectionEnd; } else { // must use a temporary form element for the selection and copy target = document.getElementById(targetId); if (!target) { var target = document.createElement("textarea"); target.style.position = "absolute"; target.style.left = "-9999px"; target.style.top = "0"; target.id = targetId; document.body.appendChild(target); } target.textContent = elem.textContent; } // select the content var currentFocus = document.activeElement; target.focus(); target.setSelectionRange(0, target.value.length); // copy the selection var succeed; try { succeed = document.execCommand("copy"); } catch(e) { succeed = false; } // restore original focus if (currentFocus && typeof currentFocus.focus === "function") { currentFocus.focus(); } if (isInput) { // restore prior selection elem.setSelectionRange(origSelectionStart, origSelectionEnd); } else { // clear temporary content target.textContent = ""; } return succeed; } </script></pre>
getUrl中調(diào)用了封裝好的copyToClipboard方法實(shí)現(xiàn)了功能。有一點(diǎn)的是html中input的樣式用style="margin-left: -9999px"進(jìn)行隱藏,因?yàn)椴恢罏槭裁从胻ype="hiden"或者display="none"去隱藏都只會獲取源代碼而不是動態(tài)的url遍歷出來的值。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- js復(fù)制文本到粘貼板(Clipboard.writeText())
- H5 js點(diǎn)擊按鈕復(fù)制文本到粘貼板
- 基于js實(shí)現(xiàn)復(fù)制內(nèi)容到操作系統(tǒng)粘貼板過程解析
- 20行JS代碼實(shí)現(xiàn)粘貼板復(fù)制功能
- JavaScript實(shí)現(xiàn)復(fù)制內(nèi)容到粘貼板代碼
- js實(shí)現(xiàn)點(diǎn)擊圖片將圖片地址復(fù)制到粘貼板的方法
- js 實(shí)現(xiàn)復(fù)制到粘貼板的功能代碼
- JS復(fù)制特定內(nèi)容到粘貼板
- Web js實(shí)現(xiàn)復(fù)制文本到粘貼板
相關(guān)文章
JS實(shí)現(xiàn)跟隨鼠標(biāo)立體翻轉(zhuǎn)圖片的方法
這篇文章主要介紹了JS實(shí)現(xiàn)跟隨鼠標(biāo)立體翻轉(zhuǎn)圖片的方法,涉及javascript操作圖片翻轉(zhuǎn)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05js實(shí)現(xiàn)點(diǎn)小圖看大圖效果的思路及示例代碼
點(diǎn)小圖看大圖的效果想必很多的朋友都有見到過吧,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10Javascript前端下載后臺傳來的文件流代碼實(shí)例
這篇文章主要介紹了Javascript前端下載后臺傳來的文件流代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08js實(shí)現(xiàn)點(diǎn)擊每個(gè)li節(jié)點(diǎn),都彈出其文本值及修改
本篇文章主要分享了js實(shí)現(xiàn)點(diǎn)擊每個(gè)li節(jié)點(diǎn),都彈出其文本值及修改的實(shí)例代碼,具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12JS技巧Canvas性能優(yōu)化臟矩形渲染實(shí)例詳解
這篇文章主要為大家介紹了JS技巧Canvas?性能優(yōu)化臟矩形渲染實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01