HTML, Wort anklicken zum kopieren?
Ich bin daran, eine eigene Website zu machen...
Wie kann man machen, das die Leser der Website (HTML) z.B auf das Wort „Hallo“ klicken und es dieses direkt für sie kopiert bzw. in die Zwischenablage nimmt? Geht das irgendwie?
1 Antwort
Vom Beitragsersteller als hilfreich ausgezeichnet
Das habe ich aus w3schools.com
<!-- The text field -->
<input type="text" value="Hello World" id="myInput">
<!-- The button used to copy the text -->
<button onclick="myFunction()">Copy text</button>
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("myInput");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
Woher ich das weiß:Recherche
Mir ist noch wichtig hinzuzufügen, dass die gewünschte Funktionalität mit HTML nicht geht, das erfordert JavaScript wie man auch im Codebeispiel sieht. Der Teil ab "function myFunction() {" bis zum Ende muss aber innerhalb von <script> -- muss hier stehen -- </script> stehen. So kann man JavaScript-Code in einer HTML-Datei einfügen.