javascript jquery点击复制代码

 

 function copyToClipboard() {
        var $temp = $("<input>");
        $("body").append($temp);
        $temp.val($('h2 > a')[0].href).select();
        document.execCommand("copy");
        $temp.remove();
    }

    $('<div class="container"><button id="btncopy">Click to copy</button></div>').insertBefore('#download');
    $('button#btncopy').css({"font-size": "150%", "width": "400px", "height": "294.4px", "background-color": "lightblue", "text-align": "center"});
    document.querySelector('button#btncopy').addEventListener ("click", copyToClipboard, false);

 

参考:https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard

注意不要使用onclick,因为onclick要求的函数,会显示没定义。

使用addEventListener,参考:https://stackoverflow.com/questions/17378199/uncaught-referenceerror-function-is-not-defined-with-onclick

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

 

posted @ 2024-05-27 15:08  profesor  阅读(59)  评论(0编辑  收藏  举报