Zakładając wszystkie style są inline, trzeba dostać html elementu zamiast tekstu. Coś jak:
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).html()).select(); //Note the use of html() rather than text()
document.execCommand("copy");
$temp.remove();
}
Edytuj na podstawie opinii:
Aby skopiować formatowanie do czegoś w rodzaju treści wiadomości Gmail lub dokumentu programu Word trzeba rzeczywiście wybrać element w postaci zakresu. Po wstawieniu treści HTML do textarea faktycznie kopiujesz nieprzetworzony tekst. Chcesz zrobić coś takiego:
function copyToClipboard(element) { //Note, element should be a node rather than a jQuery instance.
var selection = window.getSelection(), //Get the window selection
selectData = document.createRange(); //Create a range
selection.removeAllRanges(); //Clear any currently selected text.
selectData.selectNodeContents(element); //Add the desired element to the range you want to select.
selection.addRange(selectData); //Highlight the element (this is the same as dragging your cursor over an element)
var copyResult = document.execCommand("copy"); //Execute the copy.
if(copyResult) //was the copy successful?
selection.removeAllRanges(); //Clear the highlight.
else
alert("Your browser does not support clipboard commands, press ctrl+c");
}
Myślę, że to pomogłoby, gdybyś opisał, gdzie zostanie wklejony. Jeśli weźmiesz cały HTML, może nie wkleić go tak, jak chcesz. – Brian
Do wiadomości treści gmail. Po prostu chcę, aby kopiować "kliknij, aby skopiować" tak samo jak przy zaznaczaniu tekstu myszą i kopiowaniu. – FabianCannes
Widzę, jak to byłoby przydatne. Dobre pytanie. +1. – Brian