Vue自定義復(fù)制指令 v-copy功能的實現(xiàn)
使用自定義指令創(chuàng)建一個點擊復(fù)制文本功能
1. 創(chuàng)建v-copy.js文件
import Vue from "vue"; // 注冊一個全局自定義復(fù)制指令 `v-copy` Vue.directive("copy", { bind(el, { value }) { el.$value = value; el.handler = () => { el.style.position = 'relative'; if (!el.$value) { // 值為空的時候,給出提示 alert('無復(fù)制內(nèi)容'); return } // 動態(tài)創(chuàng)建 textarea 標(biāo)簽 const textarea = document.createElement('textarea'); // 將該 textarea 設(shè)為 readonly 防止 iOS 下自動喚起鍵盤,同時將 textarea 移出可視區(qū)域 textarea.readOnly = 'readonly'; textarea.style.position = 'absolute'; textarea.style.top = '0px'; textarea.style.left = '-9999px'; textarea.style.zIndex = '-9999'; // 將要 copy 的值賦給 textarea 標(biāo)簽的 value 屬性 textarea.value = el.$value // 將 textarea 插入到 el 中 el.appendChild(textarea); // 兼容IOS 沒有 select() 方法 if (textarea.createTextRange) { textarea.select(); // 選中值并復(fù)制 } else { textarea.setSelectionRange(0, el.$value.length); textarea.focus(); } const result = document.execCommand('Copy'); if (result) alert('復(fù)制成功'); el.removeChild(textarea); } el.addEventListener('click', el.handler); // 綁定點擊事件 }, // 當(dāng)傳進來的值更新的時候觸發(fā) componentUpdated(el, { value }) { el.$value = value; }, // 指令與元素解綁的時候,移除事件綁定 unbind(el) { el.removeEventListener('click', el.handler); }, });
2. 引入v-copy
// main.js 根據(jù)文件的相關(guān)路徑引入v-copy.js文件 import "xx/v-copy.js"; // v-copy 指令
3. 在標(biāo)簽使用v-copy
<span v-copy="復(fù)制內(nèi)容">復(fù)制</span>
補充:Vue 自定義指令合集 (文本內(nèi)容復(fù)制指令 v-copy)
我們常常在引入全局功能時,主要都是寫于 js 文件、組件中。不同于他們在使用時每次需要引用或注冊,在使用上指令更加簡潔。
今天主要實現(xiàn)的是一個復(fù)制文本指令*v-copy
*,其中主要的參數(shù)有兩個icon
和dblclick
參數(shù) | 說明 |
---|---|
dblclick | 雙擊復(fù)制 |
icon | 添加icon復(fù)制按鈕,單擊按鈕實現(xiàn)復(fù)制 |
代碼如下:
bind(el, binding) { if (binding.modifiers.dblclick) { el.addEventListener("dblclick", () => handleClick(el.innerText)); el.style.cursor = "copy"; } else if (binding.modifiers.icon) { el.addEventListener("mouseover", () => { if (el.hasicon) return; const icon = document.createElement("em"); icon.setAttribute("class", "demo"); icon.setAttribute("style", "{margin-left:5px,color:red}"); icon.innerText = "復(fù)制"; el.appendChild(icon); el.hasicon = true; icon.addEventListener("click", () => handleClick(el.innerText)); icon.style.cursor = "pointer"; }); el.addEventListener("mouseleave", () => { let em = el.querySelector("em"); el.hasicon = false; el.removeChild(em); }); } else { el.addEventListener("click", () => handleClick(el.innerText)); el.style.cursor = "copy"; } function handleClick(content) { if (Window.clipboardData) { window.Clipboard.setData("text", content); return; } else { (function(content) { document.oncopy = function(e) { e.clipboardData.setData("text", content); e.preventDefault(); document.oncopy = null; }; })(content); document.execCommand("Copy"); } } },
到此這篇關(guān)于Vue自定義復(fù)制指令 v-copy功能的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vue復(fù)制指令 v-copy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實現(xiàn)文字檢索時候?qū)⑺阉鲀?nèi)容標(biāo)紅功能
這篇文章主要介紹了vue中實現(xiàn)文字檢索時候?qū)⑺阉鲀?nèi)容標(biāo)紅,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08詳解vue beforeEach 死循環(huán)問題解決方法
這篇文章主要介紹了vue beforeEach 死循環(huán)問題解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02vue antd的from表單中驗證rules中type的坑記錄
這篇文章主要介紹了vue antd的from表單中驗證rules中type的坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04