vue点击复制内容

Posted on 2020-04-28 09:08  猫头唔食鱼  阅读(1603)  评论(0)    收藏  举报

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); // 删除临时实例
    },
  }

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