当前位置: 首页 > news >正文

复制所绑定元素文本的vue自定义指令

最近写了一个复制所绑定元素文本的vue自定义指令,给大家分享一下。

import { ElMessage } from 'element-plus'

// data-* 属性名
const dataCopyBtnTextAttribute = 'data-copy-btn-text'
// 复制按钮的class,结合项目实际进行设置
const copyBtnClass = 'icon-copy'

// 复制文本的方法
const copyText = data => {
  const input = document.createElement('input')
  input.setAttribute('readonly', 'readonly')
  input.setAttribute('value', data)
  input.style.position = 'absolute'
  input.style.left = '-100px'
  input.style.top = '-100px'
  input.style.opacity = '0'
  document.body.appendChild(input)
  input.select()
  document.execCommand('copy')
  setTimeout(() => {
    document.body.removeChild(input)
  })
}

// 设置 data-* 属性
function setSign(el) {
  el.setAttribute(dataCopyBtnTextAttribute, el.innerText)
}

// 移除 data-* 属性
function removeSign(el) {
  el.removeAttribute(dataCopyBtnTextAttribute)
}

// 获取 data-* 属性
function getSign(el){
  return el.getAttribute(dataCopyBtnTextAttribute)
}

// 生成复制按钮
function getCopyBtn(el){
   const iDom = document.createElement('i')
   iDom.className = `iconfont ${copyBtnClass}`
   Object.assign(iDom.style,{
    color:'blue',
    cursor:'pointer',
    marginLeft:'4px'
   })
    // 添加点击事件 
   iDom.addEventListener('click',() => {
    copyText(el.innerText)
    ElMessage({
      message: '复制成功',
      type: 'success',
      duration:1000
    })
   }) 
   return iDom
}

// 添加复制按钮
function addCopyBtn(el){
  // 当节点内容发生变化时,移除复制按钮,当复制的文本有值时,重新添加复制按钮
  if(getSign(el) !== el.innerText){
    removeCopyBtn(el)
    if(!!el.innerText){
      el.appendChild(getCopyBtn(el))
      setSign(el)
    }
  }
}

// 移除复制按钮
function removeCopyBtn(el){
  removeSign(el)
  var copyBtns = el.querySelectorAll(`.${copyBtnClass}`);
  copyBtns.forEach((element) => {
    el.removeChild(element)
    element = null
  })
}

export default {
  mounted(el, binding) {
    addCopyBtn(el)
  },
  updated(el, binding) {
    addCopyBtn(el)
  },
  beforeUnmount(el, binding){
    removeCopyBtn(el)
  }
}
http://www.dtcms.com/a/32457.html

相关文章:

  • 【论文解析】Fast prediction mode selection and CU partition for HEVC intra coding
  • flink-cdc同步数据到doris中
  • 算法的复杂性分析以及时间复杂度的表示方法
  • JavaSE学习笔记25-反射(reflection)
  • 顺序表和STL——vector【 复习笔记】
  • C++ IDE设置 visual studio 2010安装、注册、使用
  • 一周学会Flask3 Python Web开发-flask3模块化blueprint配置
  • 【Go语言快速上手】第二部分:Go语言进阶之工具与框架
  • L2-【英音】地道语音语调
  • 自由学习记录(37)
  • python学智能算法(二)|模拟退火算法:进阶分析
  • PHP 会话(Session)实现用户登陆功能
  • Flutter CupertinoNavigationBar iOS 风格导航栏的组件
  • 10-R数组
  • LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
  • 独立开发者如何寻找产品设计灵感
  • 大规模 RDMA AI 组网技术创新:算法和可编程硬件的深度融合
  • 基于Spring Boot的兴顺物流管理系统设计与实现(LW+源码+讲解)
  • eclips 快捷键
  • java方法学习
  • 探索 Peewee:轻量级 Python ORM 简明指南
  • 更改visual studio 2022 默认NuGet包路径
  • 【uni-app】对齐胶囊容器组件
  • 基于SpringBoot实现的宠物领养系统平台功能七
  • 华为guass在dbever和springboot配置操作
  • 大厂出品!三个新的 DeepSeek 平替网站
  • 【AcWing】动态规划-线性DP -选数异或
  • springboot408-基于Java的樱洵宾馆住宿管理系统(源码+数据库+纯前后端分离+部署讲解等)
  • JDK源码系列(二)
  • 第44天:Web开发-JavaEE应用反射机制类加载器利用链成员变量构造方法抽象方法