JS实现PC端全兼容复制

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <!--<script type="text/javascript" src="copy.min.js" ></script>-->
    <script>
        (function (a) {
            var b = function (e) {
                if (typeof e != "object" || !e.content) {
                    return false
                }
                var d = document.createElement("textarea");
                var c = "";
                var f = e.content;
                if (typeof f == "string") {
                    c = f
                }
                if (typeof f == "object") {
                    c = JSON.stringify(f)
                }
                if (typeof f == "function") {
                    c = f.toString()
                }
                d.value = c;
                d.style.width = "1px";
                d.style.height = "1px";
                document.body.appendChild(d);
                d.select();
                if (document.execCommand("Copy")) {
                    document.body.removeChild(d);
                    e.success && typeof e.success == "function" && e.success(c)
                } else {
                    e.error && typeof e.error == "function" && e.error()
                }
            };
            if (a.jQuery && typeof a.jQuery == "function" && typeof jQuery.prototype.extend == "function") {
                jQuery.fn.extend({
                    copy: function (c) {
                        if (!c.success || typeof c.success != "function") {
                            c.success = new Function()
                        }
                        if (!c.error || typeof c.error != "function") {
                            c.error = new Function()
                        }
                        if (c.selector && !c.content) {
                            $(this).click(function () {
                                var d = $(c.selector).html() || $(c.selector).val();
                                b({content: d, success: c.success, error: c.error})
                            })
                        } else {
                            if (!c.selector && c.content) {
                                $(this).click(function () {
                                    var d = c.content;
                                    b({content: d, success: c.success, error: c.error})
                                })
                            } else {
                                return false
                            }
                        }
                    }
                })
            }
            a.Copy = b
        })(window);
    </script>
</head>
<body>
<div class="djc">nimab44333333333333333334o132</div>
<button id="btn">复制</button>
<script>
    $("#btn").copy({
        selector: ".djc",
        success: function (txt) {
            alert("复制成功,复制内容为:" + txt)
        },
        error: function () {
            alert("复制失败,请手动Ctrl+C复制");
        }
    })
</script>
</body>
</html>

  

posted @ 2017-06-30 16:20  daxia11  阅读(216)  评论(0编辑  收藏  举报