JS 拷贝数据到系统粘贴板

copy from https://juejin.im/post/5aefeb6e6fb9a07aa43c20af

    window.Clipboard = (function(window, document, navigator) {
        var textArea,
            copy;

        // 判断是不是ios端
        function isOS() {
            return navigator.userAgent.match(/ipad|iphone/i);
        }
        //创建文本元素
        function createTextArea(text) {
            textArea = document.createElement('textArea');
            textArea.value = text;
            document.body.appendChild(textArea);
        }
        //选择内容
        function selectText() {
            var range,
                selection;

            if (isOS()) {
                range = document.createRange();
                range.selectNodeContents(textArea);
                selection = window.getSelection();
                selection.removeAllRanges();
                selection.addRange(range);
                textArea.setSelectionRange(0, 999999);
            } else {
                textArea.select();
            }
        }

        //复制到剪贴板
        function copyToClipboard() {        
            var ret = false;
            try{
                if(document.execCommand("Copy")){
                    console.log("复制成功!");  
                    ret = true;
                }else{
                    console.log("复制失败!请手动复制!");
                }
            }catch(err){
                console.log ("复制错误!请手动复制!")
            }
            document.body.removeChild(textArea);
            return ret;
        }

        copy = function(text) {
            createTextArea(text);
            selectText();
            copyToClipboard();
        };

        return {
            copy: copy
        };
    })(window, document, navigator);

    Clipboard.copy("hello world");
点击进入评论 ...