vue 复制文本

copyTXT() {
      const dom = document.getElementById(`codeTxt`);
      const selection = window.getSelection();
      const range = document.createRange();

      if (!dom || !selection || !range) return;

      // 选择复制目标
      range.selectNodeContents(dom);
      selection.removeAllRanges();
      selection.addRange(range);
      // 已复制文字
      console.log("selectedText", selection.toString());

      // 执行复制
      document.execCommand("copy");
      this.$toast("复制成功");
      // 去除所有选中
      selection.removeAllRanges();
      // 复制内容成功,通过ctrl+v粘贴使用!
    },
posted @ 2021-08-10 18:07  起风了1573  阅读(126)  评论(0)    收藏  举报