java getChildNodes() 返回的 DOM NodeList 的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/66032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:55:12  来源:igfitidea点击:

Order of DOM NodeList returned by getChildNodes()

javaxmldom

提问by Adrian Mouat

The DOM method getChildNodes()returns a NodeListof the children of the current Node. Whilst a NodeListis ordered, is the list guaranteed to be in document order?

DOM 方法getChildNodes()返回NodeList当前Node. 虽然 aNodeList是有序的,但列表是否保证按文档顺序排列?

For example, given <a><b/><c/><d/></a>is a.getChildNodes()guaranteed to return a NodeListwith b, cand din that order?

例如,假设<a><b/><c/><d/></a>a.getChildNodes()保证返回一个NodeListbcd按此顺序

The javadocisn't clear on this.

javadoc的不明确这一点。

采纳答案by sblundy

In my experience, yes. The DOM specisn't any clearer. If you're paranoid, try something like

根据我的经验,是的。该DOM规范没有任何清晰。如果你是偏执狂,试试像

current = node.firstChild;
while(null != current) {
    ...
    current = current.nextSibling;
}

回答by EBGreen

My experience is that every time that I have bothered to look it has been in document order. However, I believe that I read somewhere it is not guaranteed to be in document order. I can't find where I read that right now, so take it as hearsay. I think your best bet if you musthave them in document order would be to use FirstChild then NextSibling until there are no more sibs.

我的经验是,每次我费心去查看它时,都是按文档顺序排列的。但是,我相信我在某处阅读了它并不能保证按文档顺序排列。我现在找不到我在哪里读到的,所以把它当作道听途说。我认为如果您必须按文档顺序排列它们,最好的选择是使用 FirstChild 然后使用 NextSibling 直到没有更多的同胞。

回答by John Millikin

A document-ordered node list is the behavior in other implementations of the DOM, such as Javascript's or Python's. And a randomly-ordered node list would be utterly useless. I think it's safe to depend on nodes being returned in document order.

文档顺序节点列表是 DOM 的其他实现中的行为,例如 Javascript 或 Python。一个随机排序的节点列表将完全没用。我认为依赖以文档顺序返回的节点是安全的。

回答by Calum

I'd love to tell you that this is guaranteed (as I believe it is) but the Document Object Model specificationitself seems ambiguous in this case. I'm pretty sure that it's always document-order, though.

我很想告诉你这是有保证的(我相信是这样),但在这种情况下,文档对象模型规范本身似乎不明确。不过,我很确定它始终是文档顺序。

回答by BrewinBombers

In your example, as presented. I believe so. However, I've experienced real-world experiences where spaces have been interpreted as nodes so:

在您的示例中,如所示。我相信是这样。但是,我经历过将空间解释为节点的现实世界的经验,因此:


<a><b/><c/><d/></a>

is different than

不同于


<a><b/> <c/><d/></a>

if you're looking at index [1], firefox and IE may present different results. I would advise against relying on the order depending on your need.

如果您正在查看索引 [1],则 firefox 和 IE 可能会显示不同的结果。我建议不要根据您的需要依赖订单。

回答by houston

Yes they are ordered as it returns a nodeList, you would have to say getNamedChildNodes to get a list that is not ordered as in namedNodeList.

是的,它们在返回 nodeList 时已排序,您必须说 getNamedChildNodes 才能获取未按 namedNodeList 排序的列表。