1. 程式人生 > 其它 >記錄:vue中實現點選複製文字

記錄:vue中實現點選複製文字

技術標籤:jsvue

如題,使用document.execCommand()方法。該方法的copy與cut基本相容所有主流瀏覽器。MDN詳情點此

//複製連結
  private copyPath(link:string) {
    let copyInput = document.createElement("input");
    copyInput.value = link;
    // copyInput.setAttribute('readonly', 'readonly');//避免ios端聚焦引起的白屏抖動
    document.body.appendChild
(copyInput);//插入body copyInput.select();//選擇物件 // copyInput.setSelectionRange(0, link.length);//ios端使用setSelectionRange避免選擇不全問題 document.execCommand("Copy");//執行復制命令 this.copySuccess = true; copyInput.remove();//移除 }