admin管理员组

文章数量:1322180

How can I loop through DocumentFragment childNodes? I've tried doing the following:

console.log(result.childNodes);
console.log(result.childNodes.length);

But length seems to be 0 even if I can see the actual child nodes in firebug, like:

[div#tiptip_arrow]
0

Update:

/ This is not exactly what I'm doing, but demonstrates the problem: when I have a look in Firebug console, I see that the fragment has child nodes, but console.log(result.childNodes); yields [] for some reason. Why is that?

How can I loop through DocumentFragment childNodes? I've tried doing the following:

console.log(result.childNodes);
console.log(result.childNodes.length);

But length seems to be 0 even if I can see the actual child nodes in firebug, like:

[div#tiptip_arrow]
0

Update:

http://jsfiddle/ve5hf/ This is not exactly what I'm doing, but demonstrates the problem: when I have a look in Firebug console, I see that the fragment has child nodes, but console.log(result.childNodes); yields [] for some reason. Why is that?

Share Improve this question edited Jan 3, 2012 at 16:58 Fluffy asked Jan 3, 2012 at 16:22 FluffyFluffy 28.4k42 gold badges156 silver badges238 bronze badges 1
  • 1 of course it has no child Nodes it's an empty document fragment. Note that the reason you see data in childNodes when you log result is because console.log is live. – Raynos Commented Jan 3, 2012 at 17:33
Add a ment  | 

1 Answer 1

Reset to default 8
var o = document.createDocumentFragment();
o.appendChild(document.createElement('div'));
var childNodes = o.childNodes;
for (var i = 0, len = childNodes.length; i < len; i++) {
  console.log(childNodes[i].tagName);
}

.length does work.

We need more specifics. How do you create the document fragment? What browser?

本文标签: javascriptHow to loop through DocumentFragment39s childrenStack Overflow