阅读 12

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);
            }
    ```复制代码
关注下面的标签,发现更多相似文章
评论