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>

  

转载于:https://www.cnblogs.com/znj211985211/p/7099453.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值