瀏覽器裏點擊複製到剪貼板的小方法

今天分享一個在瀏覽器裏點擊複製到剪貼板的小方法,經測試全部瀏覽器都沒有兼容性問題,包括安卓和iOS,拿走不謝!瀏覽器

相似於這種需求:

 

 

 

現成的代碼:
function copy() {
    const input = document.createElement('input');
    input.value = '想要複製的內容';
    document.body.appendChild(input);
    input.select();
    if (document.execCommand('Copy')) {
        document.execCommand('Copy');
        console.log('複製成功!');
    } else {
        console.log('複製失敗!');
    }
    input.style.display = 'none';
}

  還沒兩天呢,就被QA報了bug,360奇酷手機不支持這種方法複製,頓時被啪啪打臉,又實在不想動用原生的方法,因此最後只好妥協了增長長按進行復制的方法,只對該dom增長一個屬性便可,user-select:all;app

  H5的道路任重而道遠啊,如今相比於WX和RN落後太多了,且行且珍惜吧!dom

相關文章
相關標籤/搜索