JavaScript でテキストをクリップボードへコピーする方法

JavaScript でテキストをクリップボードへコピーする方法:

クリップボードにコピーする機能のサンプルとコード

<html> 
    <head> 
        <meta charset="UTF-8"> 
        <script> 
            function copyTextToClipboard(element) { 
                var copyTarget = document.getElementById(element); 
                var copyArea = document.createElement("input"); 
                copyArea.value = copyTarget.textContent; 
                document.body.appendChild(copyArea); 
                copyArea.select(); 
                document.execCommand("Copy"); 
                if (!('remove' in Element.prototype)) { 
                    Element.prototype.remove = function () { 
                        if (this.parentNode) { 
                            this.parentNode.removeChild(this); 
                        } 
                    }; 
                } 
                copyArea.remove(); 
                alert(copyTarget.textContent + "をコピーした。"); 
            } 
        </script> 
    </head> 
    <body> 
        <span id="copyTarget">テキスト</span>  
        <button onclick="copyTextToClipboard('copyTarget')">コピー</button> 
    </body> 
</html> 
 

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)