h5實現一鍵複製到粘貼板 兼容ios

實現原理

採用document.execCommand('copy')來實現複製到粘貼板功能ios

複製必須是選中input框的文字內容,而後執行document.execCommand('copy')命令實現複製功能。
初步實現方案git

const input = document.querySelector('#copy-input');
    if (input) {
      input.value = text;
      if (document.execCommand('copy')) {
        input.select();
        document.execCommand('copy');
        input.blur();
        alert('已複製到粘貼板');
      }
    }

兼容性問題

  1. input 輸入框不能hidden或者display: none;
    若是須要隱藏輸入框能夠使用定位脫離文檔流,而後移除屏幕
#copy-input{
position: absolute;
left: -1000px;
z-index: -1000;
}

2.ios下不能執行document.execCommand('copy')github

在ios設備下alert(document.execCommand('copy'))一直返回false
查閱相關資料發現ios下input不支持input.select();chrome

所以拷貝的文字必須存在,不能爲空字符串,否則也不會執行復制空字符串的功能

參考這篇博客實現了ios下複製的功能 https://blog.csdn.net/VLilyV/...瀏覽器

主要是使用textbox.createTextRange方法選中輸入框的文字微信

// input自帶的select()方法在蘋果端沒法進行選擇,因此須要本身去寫一個相似的方法
// 選擇文本。createTextRange(setSelectionRange)是input方法
function selectText(textbox, startIndex, stopIndex) {
  if (textbox.createTextRange) {//ie
    const range = textbox.createTextRange();
    range.collapse(true);
    range.moveStart('character', startIndex);//起始光標
    range.moveEnd('character', stopIndex - startIndex);//結束光標
    range.select();//不兼容蘋果
  } else {//firefox/chrome
    textbox.setSelectionRange(startIndex, stopIndex);
    textbox.focus();
  }
}

3.ios設備上覆制會觸發鍵盤彈出事件app

給input加上readOnly只讀屬性測試

代碼

踩完以上的坑,總結的代碼以下
git地址 https://github.com/zhaosheng8...網站

copyText = (text) => {
    // 數字沒有 .length 不能執行selectText 須要轉化成字符串
    const textString = text.toString();
    let input = document.querySelector('#copy-input');
    if (!input) {
      input = document.createElement('input');
      input.id = "copy-input";
      input.readOnly = "readOnly";        // 防止ios聚焦觸發鍵盤事件
      input.style.position = "absolute";
      input.style.left = "-1000px";
      input.style.zIndex = "-1000";
      document.body.appendChild(input)
    }

    input.value = textString;
    // ios必須先選中文字且不支持 input.select();
    selectText(input, 0, textString.length);
    console.log(document.execCommand('copy'), 'execCommand');
    if (document.execCommand('copy')) {
      document.execCommand('copy');
      alert('已複製到粘貼板');
    }
    input.blur();

    // input自帶的select()方法在蘋果端沒法進行選擇,因此須要本身去寫一個相似的方法
    // 選擇文本。createTextRange(setSelectionRange)是input方法
    function selectText(textbox, startIndex, stopIndex) {
      if (textbox.createTextRange) {//ie
        const range = textbox.createTextRange();
        range.collapse(true);
        range.moveStart('character', startIndex);//起始光標
        range.moveEnd('character', stopIndex - startIndex);//結束光標
        range.select();//不兼容蘋果
      } else {//firefox/chrome
        textbox.setSelectionRange(startIndex, stopIndex);
        textbox.focus();
      }
    }
  };


// 複製文字

// 必須手動觸發 點擊事件或者其餘事件,不能直接使用js調用!!!
copyText('h5實現一鍵複製到粘貼板 兼容ios')


/*兼容性補充:
 移動端:
 安卓手機:微信(chrome)和幾個手機瀏覽器均可以用。 
 蘋果手機:微信裏面和sarafi瀏覽器裏也均可以,  
 PC:sarafi版本必須在10.2以上,其餘瀏覽器能夠.
 兼容性測試網站:https://www.caniuse.com/
*/
        
 .
相關文章
相關標籤/搜索