vue点击复制内容

html

  <p ref="p">这是文字</p>
  <button @click="copy">点击复制上面内容</button>

js

  data() {
    return {
      copyText: ""
    };
  },
  methods: {
    copy() {
      this.copyText = this.$refs.p.innerText;
       var input = document.createElement("input"); // 直接构建input
      input.value = this.copyText; // 设置内容
      console.log(input.value);

      document.body.appendChild(input); // 添加临时实例
      input.select(); // 选择实例内容
      document.execCommand("Copy"); // 执行复制
      document.body.removeChild(input); // 删除临时实例
    },
  }

也可以长按复制内容,长按事件,看上一篇。

原文地址:https://www.cnblogs.com/luguankun/p/12791855.html