JavaScript - Node.children和Node.childNodes的区别

写到这个也是因为群里的学长面试的时候遇到了这个问题,然后顺手分享了一下

首先是MDN里面对两个API的解释

children

Node.children is a read-only property that returns a live HTMLCollection of the child elements of Node.

childNodes

The Node.childNodes read-only property returns a live collection of child nodes of the given element where the first child node is assigned index 0.

所以children会返回你查找的节点里所有element子节点,而childNodes会返回所有element、文本、注释之类的节点,大概这样↓

 

posted @ 2017-07-02 00:15  osyo  阅读(891)  评论(0编辑  收藏  举报