Children and childnodes
1. childnodes attributes, Standard. It returns the child element set of the specified element, including the HTML node, all attributes, and text. You can use nodetype to determine the type of node. Only when nodetype = 1 is the element node, 2 is the attribute node, and 3 is the text node.
Some people mistakenly use () to retrieve the set element. The following table lists the support of various browsers for childnodes (I:
|
Internet Explorer 6/7/8/Safari/Chrome/Opera |
Ie9/Firefox |
Childnodes (I) |
Supported |
Not Supported |
Sometimes the first HTML subnode (non-attribute/text node) of the specified element needs to be obtained, and the firstchild attribute is the most likely to come to mind. If there is a line break or space before the first HTML node in the code, then firstchild does not return what you want. You can use nodetype to determine.
123456 |
function getFirst(elem){ for ( var i=0,e;e=elem.childNodes[i++];){ if (e.nodeType==1) return e; } } |
2. Children attributes, Non-standard. It returns the child element set of the specified element. After testing, it only returns HTML nodes, or even does not return text nodes. In addition, it is surprisingly consistent across all browsers. Like childnodes, the set element () is not supported in Firefox. Therefore, if you want to obtain the first HTML node of the specified element, you can use children [0] to replace the above getfirst function. Note that children includes annotation nodes in IE.
Differences between children and childnodes