HTML5 alternative to flash-based ZeroClipboard for safe copying of data to clipboard?

前端 未结 6 1479
春和景丽
春和景丽 2020-12-02 14:22

With flash on the way out in many environments (iPhone, Android, IE10, etc...), is there any new solution forthcoming in any browsers that will allow a safe copy of informat

6条回答
  •  再見小時候
    2020-12-02 14:27

    There are great answers to this question, and I chose to use this snippet:

    function copyToClipboard(element) {
        var $temp = $("");
        $("body").append($temp);
        $temp.val($(element).text()).select();
        document.execCommand("copy");
        $temp.remove();
    }
    

    However, if there's bootstrap-select on your page, the $temp.val($(element).text()).select() line will throw an error:

    Widget can only work on select elements

    You can use .trigger('select') instead, as stated in the jQuery documentation for .select(), like this:

    $temp.val($(element).val()).trigger('select');
    

提交回复
热议问题