开发者

DOM: element created with cloneNode(true) missing element when added to DOM

开发者 https://www.devze.com 2023-01-01 08:18 出处:网络
I\'m creating a tree control and I\'m attempting to use a parent element as a template for its children.To this end I\'m using the element.cloneNode(true) method to deep clone the parent element.Howev

I'm creating a tree control and I'm attempting to use a parent element as a template for its children. To this end I'm using the element.cloneNode(true) method to deep clone the parent element. However when I insert the cloned element into the DOM it is missing certain inner elements despite having an outerHTML value identical to its parent. Surprisingly I observe the same behavior is in I开发者_Python百科E, Firefox, and Chrome leading me to believe that it is by design.

This is the HTML for the node I'm attempting to clone.

<SPAN class=node><A class=nodeLink href="/SparklerRestService2.aspx?q={0}" name=http://dbpedia.org/data/Taylor_Swift.rdf>
<IMG class=nodeIcon alt="Taylor Swift" src="images/node.png"><SPAN class=nodeText>Taylor Swift</SPAN></A><SPAN class=nodeDescription>Taylor Swift is a swell gall who is realy great.</SPAN></SPAN>

Once I've cloned the node using cloneNode(true) I examine the outerHTML property and find that it is indeed identical to the original.

<SPAN class=node><A class=nodeLink href="/SparklerRestService2.aspx?q={0}" name=http://dbpedia.org/data/Taylor_Swift.rdf><IMG class=nodeIcon alt="Taylor Swift" src="images/node.png"><SPAN class=nodeText>Taylor Swift</SPAN></A><SPAN class=nodeDescription>Taylor Swift is a swell gall who is realy great.</SPAN></SPAN>

However when I insert it into the DOM and inspect the result using FireBug I find that the element has been transformed:

<span class="node" style="top: 0px; left: 0px;"<a class=nodeLink href="/SparklerRestService2.aspx?q={0}" name=http://dbpedia.org/data/Taylor_Swift.rdf>Taylor Swift</a><span class="nodeDescription">Taylor Swift is a swell gall who is realy great.</span></span>

Notice that the grandchildren of the node (the image tag and the span tag surrounding "Taylor Swift") are missing, although strangely the great grandchild "Taylor Swift" text node has made it into the tree.

Can anyone shed some light on this behavior? Why would nodes disappear after insertion into the DOM, and why am I seeing the same result in all three major browser engines?


I tried it and it works fine. I notice you say that the text changed to say "It's great" which wasn't even in the original. Sorry but I'm not buying that.

0

精彩评论

暂无评论...
验证码 换一张
取 消