web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. AIGC工具
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    手机应用

vuejs如何实现复制粘贴功能

882 ℃

vuejs如何实现复制粘贴功能,下面web建站小编给大家详细介绍一下实现代码!

新建copy.js的文件

const vCopy = {
  bind(el, { value }) {
    el.$value = value;
    el.handler = () => {
      if (!el.$value) {
       console.log('无复制内容');
        return;
      }
      const textarea = document.createElement('textarea');
      textarea.readOnly = 'readonly';
      textarea.style.position = 'absolute';
      textarea.style.left = '-9999px';

      textarea.value = el.$value;
      document.body.appendChild(textarea);
      // 选中值并复制
      textarea.select();
      textarea.setSelectionRange(0, textarea.value.length);
      const result = document.execCommand('Copy');
      if (result) {
        console.log('复制成功');
      }
      document.body.removeChild(textarea);
    };

    el.addEventListener('click', el.handler);
  },

  componentUpdated(el, { value }) {
    el.$value = value;
  },

  unbind(el) {
    el.removeEventListener('click', el.handler);
  },
};
export default vCopy;

新建directives.js文件

import copy from './copy.js';

// 自定义指令
const directives = {
  copy,
};

export default {
  install(Vue) {
    Object.keys(directives).forEach((key) => {
      Vue.directive(key, directives[key]);
    });
  },
};

main.js全局引入

import Vue from 'vue';
import Directives from './directives';
Vue.use(Directives);

页面调用

<template>
  <div >
    <button v-copy="copyText">拷贝</button>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        copyText:'要copy的内容'
      }
    },
    methods: {
      init () {
      },
    },
    mounted () {
      _this = this;
      _this.init();
    },
  }
</script>

Excel如何实现数据的复制粘贴

vuejs实现el-input复制粘贴功能

javaScript如何复制粘贴指定标签中的内容

苹果笔记本黏贴失败解决方法

标签: 复制粘贴

上面是“vuejs如何实现复制粘贴功能”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3057.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > vuejs
本文共计1179个字,预计阅读时长8分钟
Trae:新一代免费的AI编程工具

基金从业资格考试题库

一站式备考基金从业资格考试,收录2021-2025年模拟题库!呱呱工具箱

AI工作站

收录全球3800+ 款各行各业AI应用,轻轻松松做事!
生活小工具,收录了80多款小工具
上一篇: 抖音联合方正打造免费可商用品牌字体——抖音美好体
下一篇: 推荐一个在线图文转视频、AI 数字人工具——一帧秒创(免费赠送100分钟)
x 打工人ai神器