HTML DOMのexecCommand()メソッドの、テキストをクリップボードにコピーする簡単な方法。
JavaScriptが最も簡単に利用可能です。
execCommand()は、ドキュメントの選択されたセクションに対して特定のコマンドを実行します。
このcopyコマンドをdocument.execCommand()で使用して、JavaScriptを使用してテキストコンテンツをクリップボードにコピーできます。
サンプルコードスニペットでは、JavaScriptを使用してテキストをクリップボードにコピーする方法を示します。
次のコードは、execCommandおよびJavaScriptを使用して、ボタンのクリック時に入力テキストフィールドからクリップボードにテキストをコピーします。
HTML
< input type = "text" value = "Welcome to CodexWorld" id = "textInput" >
< button onclick = "copyText()" > Copy text
Javascript
<script>
function copyText(){
var text = document.getElementById("textInput");
text.select();
document.execCommand("copy");
alert("Copied the text: " + text.value);
}
</script>