Wednesday, 14 August 2013

Can I keep a reference to a document fragment?

Can I keep a reference to a document fragment?

I'm playing around with a document fragment. I find it hard to understand
how it behave when I append it to the DOM.
I create a doc fragment that I assign to a variable, when I insert some
stuff into it, and append the doc fragment into a element. But if I clear
the element my variable which should reference to the doc fragment contain
an empty document fragment.
I trying to make a cache for a third party lib that creates document
fragments. So I would like to get this working. Should I create a
cloneNode before I append the fragment to the DOM, is that correct?
I have created a JS fiddle: http://jsfiddle.net/4CTXG/1/
var test = document.createDocumentFragment();
//var test = document.createElement("div"); // This one work
$(test).append($("<div>").html('Hello world!'));
$("#result").append(test);
setTimeout(function(){
$("#result").children().remove();
$("#result").append(test);
console.log('Now test should have been appended');
$(result).css({"background": "#FF0000"});
},5000)

No comments:

Post a Comment