Hallo Leute,
ihr wisst, wann man strg+c drückt auf einer Webseite, dann kopiert man die ganze Webseite samt Formatierung (im Gegensatz zum unformatierten Text) in die Zwischenablage.
Diese kann man dann zum Beispiel im Mail Client oder in Word einfügen mit strg+v.
Nun möchte ich extra ein Button dafür erstellen überhalb der Seite, der das mit einem Button macht.
Ich habe es teilweise geschafft, aber nur wird der Text nicht formatiert und die CSS wird buchstäblich mitgenommen.
Der Code sieht beispielsweise so aus.
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml"><head><script>function copyToClipboard(element){var $temp = $("<input>");$("body").append($temp);$temp.val($(element).text()).select();document.execCommand("copy");$temp.remove();}</script><link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><center><button onclick="copyToClipboard('.body1')">Copy TEXT 1</button></center><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=0.7, minimum-scale=0.7, user-scalable=no"><meta http-equiv="X-UA-Compatible" content="IE=edge" /><title>Test</title> <style type="text/css">.p1 {font: 30px arial, sans-serif;}</style></head><body class="body1"><p class="p1">This is a text in the main body of this file!</p></body></html>
Aber wenn ich den Text jetzt über den Button kopiere, habe ich zwei nicht gewollte Sachen.
1.) Die Formatierung wird strikt und einfach ignoriert, d.h. wenn ich den Inhalt in Word eintrage, dann wird keine Formatierung angewendet, sondern als reiner Text geschrieben. Wenn ich aber manuell kopiere, dann wird die Formatierung mit übernommen.
2.) Obwohl der Script, sowie der Button nicht im Body der Email ist, wird "Copy TEXT 1Test .p1 {font: 30px arial, sans-serif;}This is a text in the main body of this file!" angezeigt, also somit die Buttonaufschrift. Es sollte normalerweise nicht so sein. Sondern nur "This is a text in the main body of this file!" kopiert werden mit der Formatierung, dass 30px, arial/sans-serif ist.