// copy文本
handleLinkCopy(data) {
const range = document.createRange(); // 創(chuàng)建range對(duì)象
range.selectNode(document.getElementById(data.id)); //獲取復(fù)制內(nèi)容的 id 選擇器
const selection = window.getSelection(); //創(chuàng)建 selection對(duì)象
if (selection.rangeCount > 0) selection.removeAllRanges(); //如果頁(yè)面已經(jīng)有選取了的話,會(huì)自動(dòng)刪除這個(gè)選區(qū),沒(méi)有選區(qū)的話腿箩,會(huì)把這個(gè)選取加入選區(qū)
selection.addRange(range); //將range對(duì)象添加到selection選區(qū)當(dāng)中粱甫,會(huì)高亮文本塊
document.execCommand('copy'); //復(fù)制選中的文字到剪貼板
this.$message.success('復(fù)制成功')
selection.removeRange(range); // 移除選中的元素
},
僅文本
// copy 生成鏈接
handleLinkCopy(data) {
return new Promise((resolve) => {
var input = document.createElement("input"); // js創(chuàng)建一個(gè)input輸入框
input.value = data.id; // 將需要復(fù)制的文本賦值到創(chuàng)建的input輸入框中
document.body.appendChild(input); // 將輸入框暫時(shí)創(chuàng)建到實(shí)例里面
input.select(); // 選中輸入框中的內(nèi)容
document.execCommand("Copy"); // 執(zhí)行復(fù)制操作
document.body.removeChild(input); // 最后刪除實(shí)例中臨時(shí)創(chuàng)建的input輸入框贝或,完成復(fù)制操作
this.$message.success('復(fù)制成功')
console.log("點(diǎn)擊copy知識(shí)鏈接", data)
resolve();
});
},
可以自定義內(nèi)容