0

我非常喜欢在我的网站上复制到剪贴板,这里是我感兴趣的示例。

(function() {

    "use strict";

    function copyToClipboard(elem) {

        var target = elem;

        // select the content
        var currentFocus = document.activeElement;

        target.focus();
        target.setSelectionRange(0, target.value.length);
        // copy the selection
        var succeed;

        try {
            succeed = document.execCommand("copy");
        } catch (e) {
            console.warn(e);
            succeed = false;
        }
        // Restore original focus
        if (currentFocus && typeof currentFocus.focus === "function") {
            currentFocus.focus();
        }

        if (succeed) {
            $(".copied").animate({
                top: -25,
                opacity: 0
            }, 700, function() {
                $(this).css({
                    top: 0,
                    opacity: 1
                });
            });
        }
        return succeed;
    }

    $("#copyButton, #copyTarget").on("click", function() {

        copyToClipboard(document.getElementById("copyTarget"));
    });
}());

这是代码笔https://codepen.io/LattyS/pen/QvGyKL

但我有两个问题

  1. 如果我单击任何一个将它们全部复制在一起,则尝试制作多个链接时。
  2. 如何也可以创建 DIV

所以我需要同时创建 10 个以上的链接,如果有能力使用 DIV 复制到剪贴板,或者不能使用同一个项目?像图片中的这个https://gulfupload.com/i/00025/fq8kg0ef7gw6.png

4

1 回答 1

1
(function () {

    "use strict";

    function copyToClipboard(elem) {

        var target = elem;

        // select the content
        var currentFocus = document.activeElement;

        target.focus();
        target.setSelectionRange(0, target.value.length);

        // copy the selection
        var succeed;

        try {

            succeed = document.execCommand("copy");
        } catch (e) {

            console.warn(e);

            succeed = false;
        }

        // Restore original focus
        if (currentFocus && typeof currentFocus.focus === "function") {

            currentFocus.focus();
        }

        if (succeed) {

            $(target).closest('.input-group').find('.copied').animate({top: -25, opacity: 0}, 700, function () {

                $(this).css({top: 0, opacity: 1});
            });
        }

        return succeed;
    }


    $(".copyButton").on("click", function () {

        var parent = $(this).closest('.input-group');

        copyToClipboard(parent.find(".copyTarget")[0]);
    });
}());

用于从多个链接一次复制单个链接的 Codepen 链接:https ://codepen.io/anon/pen/ELXWaG

于 2018-05-03T05:27:13.500 回答