vue使用自定義指令實(shí)現(xiàn)一鍵復(fù)制功能
1.使用document.execCommand(棄用)
import { Message } from 'ant-design-vue';
const vCopy = { //
/*
bind 鉤子函數(shù),第一次綁定時(shí)調(diào)用,可以在這里做初始化設(shè)置
el: 作用的 dom 對(duì)象
value: 傳給指令的值,也就是我們要 copy 的值
*/
bind(el, { value }) {
el.$value = value; // 用一個(gè)全局屬性來存?zhèn)鬟M(jìn)來的值,因?yàn)檫@個(gè)值在別的鉤子函數(shù)里還會(huì)用到
el.handler = () => {
if (!el.$value) {
// 值為空的時(shí)候,給出提示,我這里的提示是用的 ant-design-vue 的提示,你們隨意
Message.warning('無(wú)復(fù)制內(nèi)容');
return;
}
// 動(dòng)態(tài)創(chuàng)建 textarea 標(biāo)簽
const textarea = document.createElement('textarea');
// 將該 textarea 設(shè)為 readonly 防止 iOS 下自動(dòng)喚起鍵盤,同時(shí)將 textarea 移出可視區(qū)域
textarea.readOnly = 'readonly';
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
// 將要 copy 的值賦給 textarea 標(biāo)簽的 value 屬性
textarea.value = el.$value;
// 將 textarea 插入到 body 中
document.body.appendChild(textarea);
// 選中值并復(fù)制
textarea.select();
// textarea.setSelectionRange(0, textarea.value.length);
const result = document.execCommand('copy');
if (result) {
Message.success('復(fù)制成功');
}
document.body.removeChild(textarea);
};
// 綁定點(diǎn)擊事件,就是所謂的一鍵 copy 啦
el.addEventListener('click', el.handler);
},
// 當(dāng)傳進(jìn)來的值更新的時(shí)候觸發(fā)
componentUpdated(el, { value }) {
el.$value = value;
},
// 指令與元素解綁的時(shí)候,移除事件綁定
unbind(el) {
el.removeEventListener('click', el.handler);
},
};
export default vCopy;<template>
<div>
<input v-model="text" placeholder="輸入要復(fù)制的內(nèi)容" />
<button v-copy="text">點(diǎn)擊復(fù)制</button>
</div>
</template>
<script>
import vCopy from './directives';
export default {
data () {
return {
text: '這是要復(fù)制的內(nèi)容' // 默認(rèn)的復(fù)制內(nèi)容
};
},
directives: {
copy: vCopy
}
};
</script>- 自定義指令 v-copy 綁定到按鈕上,點(diǎn)擊按鈕時(shí)會(huì)復(fù)制綁定的內(nèi)容。
- el.$value 保存了要復(fù)制的內(nèi)容,在每次點(diǎn)擊時(shí)通過 document.execCommand(‘copy’) 復(fù)制到剪貼板。
- 當(dāng)輸入框內(nèi)容變化時(shí),componentUpdated 鉤子會(huì)更新復(fù)制的內(nèi)容。
execCommand 是一種用于執(zhí)行與用戶操作相關(guān)的命令的方法,主要用于在文檔上執(zhí)行剪貼板操作(如復(fù)制、剪切、粘貼)或格式化操作(如加粗、斜體、下劃線)。還可以使用:“copy”、“cut”、“paste”、“bold”、“italic”
textarea標(biāo)簽:
- 支持選中和復(fù)制操作:textarea 標(biāo)簽的內(nèi)容可以被瀏覽器原生地選中,并且它能夠被復(fù)制到剪貼板。復(fù)制操作依賴于選中文本,而 textarea 和 input 元素是 HTML 中可以輕松選中文本內(nèi)容的表單元素。雖然 input 標(biāo)簽也可以用于復(fù)制文本,但相比于 textarea,input 標(biāo)簽有一些局限性,特別是在處理較長(zhǎng)文本或多行文本時(shí)。
- textarea 標(biāo)簽支持 readonly 屬性,可以確保它的內(nèi)容在被復(fù)制之前不會(huì)被修改。將 textarea 設(shè)置為 readonly 能避免在移動(dòng)設(shè)備上點(diǎn)擊時(shí)喚起虛擬鍵盤,避免不必要的用戶干擾。
2.Clipboard API
Clipboard API 是一個(gè)現(xiàn)代的 Web API,用于在網(wǎng)頁(yè)上執(zhí)行剪貼板操作(如復(fù)制和粘貼)。與 document.execCommand 不同,Clipboard API 提供了更現(xiàn)代的異步接口,并且支持在 HTTPS 頁(yè)面上執(zhí)行這些操作以確保安全性。
Clipboard API 的方法返回一個(gè) Promise,這意味著你可以使用 then 和 catch 方法來處理成功和失敗的情況。
實(shí)現(xiàn)一鍵粘貼的功能:
使用 navigator.clipboard.readText() 方法從剪貼板讀取文本:
let vCopy = {
bind (el, { value }) {
el.$value = value;
el.handler = async () => {
if (!el.$value) {
console.log('沒有要復(fù)制的內(nèi)容');
return;
}
try {
// 直接使用 Clipboard API 復(fù)制
await navigator.clipboard.writeText(el.$value);
console.log('復(fù)制成功');
} catch (err) {
console.error('復(fù)制失敗', err);
}
};
// 監(jiān)聽點(diǎn)擊事件
el.addEventListener('click', el.handler);
},
componentUpdated (el, { value }) {
el.$value = value;
},
unbind (el) {
el.removeEventListener('click', el.handler);
}
};
export default vCopy;<template>
<div>
<input v-model="text" placeholder="輸入要復(fù)制的內(nèi)容" />
<button v-copy="text">點(diǎn)擊復(fù)制</button>
</div>
</template>
<script>
import vCopy from './directives';
export default {
data () {
return {
text: '這是要復(fù)制的內(nèi)容' // 默認(rèn)的復(fù)制內(nèi)容
};
},
directives: {
copy: vCopy
}
};
</script>navigator 是 window 對(duì)象的一個(gè)屬性,提供了有關(guān)用戶瀏覽器的信息和一些特定的功能。
主要功能:
- 提供有關(guān)用戶代理(瀏覽器)和操作系統(tǒng)的信息。
- 提供瀏覽器的語(yǔ)言設(shè)置和在線狀態(tài)等信息。提供對(duì)剪貼板操作、地理位置、媒體設(shè)備等現(xiàn)代瀏覽器功能的訪問。
- 拓展: 使用 navigator.clipboard.writeText() 方法將文本寫入剪貼板:
<template>
<div>
<input v-model="text" placeholder="輸入要復(fù)制的內(nèi)容" />
<button @click="copyToClipboard">點(diǎn)擊復(fù)制</button>
</div>
</template>
<script>
export default {
data() {
return {
text: '默認(rèn)要復(fù)制的內(nèi)容'
};
},
methods: {
copyToClipboard() {
navigator.clipboard.writeText(this.text).then(() => {
console.log('文本已成功復(fù)制到剪貼板');
}).catch(err => {
console.error('復(fù)制失敗', err);
});
}
}
};
</script>到此這篇關(guān)于vue使用自定義指令實(shí)現(xiàn)一鍵復(fù)制的文章就介紹到這了,更多相關(guān)vue一鍵復(fù)制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-router 2.0 跳轉(zhuǎn)之router.push()用法說明
這篇文章主要介紹了vue-router 2.0 跳轉(zhuǎn)之router.push()用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue實(shí)現(xiàn)前端按鈕組件權(quán)限管理
這篇文章主要為大家介紹了vue實(shí)現(xiàn)前端按鈕組件權(quán)限管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
vue插件draggable實(shí)現(xiàn)拖拽移動(dòng)圖片順序
這篇文章主要為大家詳細(xì)介紹了vue插件draggable實(shí)現(xiàn)拖拽移動(dòng)圖片順序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
基于vue+face-api.js實(shí)現(xiàn)前端人臉識(shí)別功能
基于face-api.js要實(shí)現(xiàn)人臉識(shí)別功能,首先要將自己需要的模型文件下載保存在靜態(tài)目錄下,可以通過cdn的方式在index.html中引入face-api.js,本文給大家介紹vue+face-api.js實(shí)現(xiàn)前端人臉識(shí)別功能,感興趣的朋友一起看看吧2023-12-12
vue利用openlayers實(shí)現(xiàn)動(dòng)態(tài)軌跡
這篇文章主要為大家介紹了vue利用openlayers實(shí)現(xiàn)動(dòng)態(tài)軌跡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Vuerouter的beforeEach與afterEach鉤子函數(shù)的區(qū)別
本文詳細(xì)的介紹了Vuerouter的beforeEach與afterEach鉤子函數(shù)的區(qū)別和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
深入理解Vue keep-alive及實(shí)踐總結(jié)
這篇文章主要介紹了深入理解Vue keep-alive及實(shí)踐總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

