Use only: frag.querySelector('*').innerHTML
The asterisk selects any first DOM object found inside the fragment, which in the case is the root, and then with the root DOM object in the hands it is possible to use innerHTML
, or outerHTML
if applicable.
EDIT
If your fragment contains multiple root elements, then use querySelectorAll
:
var conteudo = "";
var all = frag.querySelectorAll('*');
for (var i = 0; i < all.length; i++)
if (!all[i].parentElement)
conteudo += all[i].outerHTML;
// mostrando o output na página
$("#output").text($("#output").text() + conteudo);
querySelectorAll is well supported in browsers:
link
EDIT 2: A more performative solution using querySelector
instead of querySelectorAll
:
var conteudo = "";
var current = frag.querySelector('*');
while (current) {
conteudo += current.outerHTML + "\n";
current = current.nextSibling;
}
EDIT 3: As the previous options did not work in IE8, I kept searching until I found the following alternative (which works on all browsers I've tested):
var conteudo = "";
var rootNodes = frag.childNodes;
for (var i = 0; i < rootNodes.length; i++)
conteudo += rootNodes[i].outerHTML;