DOM node child collections--what's the difference?

送分小仙女□ 提交于 2021-02-18 22:34:49

问题


What's the difference between the children and childNodes collections of a node? And childElementCount and childNodes.length?


回答1:


element.childNodes and element.childNodes.length: includes child elements, text nodes (including whitespace nodes between elements) and comments (plus potentially CDATASections, ProcessingInstructions, DocumentTypes and EntityReferences, depending on the document and parser). Defined by the DOM Level 1 Core standard and has been available on every browser for many years.

element.children and element.childElementCount: includes only element nodes, so can be simpler to handle in some cases. childElementCount is defined by the Element Traversal API standard and is available in the latest versions of Mozilla, Opera and WebKit. children is not defined by any standard, but is available in IE and up-to-date versions of Mozilla, Opera, WebKit and IE.




回答2:


Children contains only elements. ChildNodes contains all nodes.



来源:https://stackoverflow.com/questions/2260583/dom-node-child-collections-whats-the-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!