h5文本復制


實現功能:復制指定文字到粘貼板

(注:IOS UIWebView 禁用了 copy、paste、cut命令所以無法執行復制)

 

 實現:(兼容ios 、android 移動設備瀏覽器,除了IOS UIWebView)

copyText:function (text) {
        var textString = text.toString();
        var input = document.querySelector('#copy-input');

        // input自帶的select()方法在蘋果端無法進行選擇,自己實現
        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;

        var selectText = function(textbox, startIndex, stopIndex) {
            if (textbox.createTextRange) {
              var range = textbox.createTextRange();
              range.collapse(true);
              range.moveStart('character', startIndex);          //起始光標
              range.moveEnd('character', stopIndex - startIndex);//結束光標
              range.select();                                    //不兼容蘋果
            } else {
              textbox.setSelectionRange(startIndex, stopIndex);
              textbox.focus();
            }
        }

        selectText(input, 0, textString.length);

        //TODO IOS UIWebView禁用復制命令
        if (document.execCommand('copy')) {
          document.execCommand('copy');
        } else {
          console.log('不兼容');
        }

        input.blur();
    }

注:目前我們游戲是通過app交互來執行復制。繞開IOS UIWebView無法執行復制命令


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM