h5複製剪切板

angular serve

;
(function(app) {
    app.service('ClipBoard', ['URL', '$rootScope',
        function(URL, $rootScope) {

            //定義函數
            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);
                textArea.setAttribute('readonly', 'readonly');
            }
            //選擇內容
            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() {
                try {
                    if (document.execCommand("Copy")) {
                        $rootScope.$emit('show-alert', '複製成功');
                    } else {
                        $rootScope.$emit('show-alert', '複製失敗!請手動複製!');
                    }
                } catch (err) {
                    $rootScope.$emit('show-alert', '複製失敗!請手動複製!');
                }
                document.body.removeChild(textArea);
            }

            this.copy = function(text) {
                createTextArea(text);
                selectText();
                copyToClipboard();
            };
        }
    ])
})(app);


複製代碼

原生js

function copyFun() {
                //定義函數
                window.Clipboardes = (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);
                        textArea.setAttribute('readonly', 'readonly');
                    }
                    //選擇內容
                    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() {
                        try {
                            if (document.execCommand("Copy")) {
                                $rootScope.$emit('show-alert', '複製成功');
                            } else {
                                $rootScope.$emit('show-alert', '複製失敗!請手動複製!');
                            }
                        } catch (err) {
                            $rootScope.$emit('show-alert', '複製失敗!請手動複製!');
                        }
                        document.body.removeChild(textArea);
                    }

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

                    return {
                        copy: copy
                    };
                })(window, document, navigator);
            }
    ```複製代碼
相關文章
相關標籤/搜索