This article will introduce to youjs about the acquisition of Dom's childNodes and childrens and related nodes. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
DOM-Document Object Model
Node Type
Each DOM node has a nodeType attribute to represent the type of the node. There are 12 types in total, the common ones are ELEMENT_NODE=1;ATTRIBUTE_NODE=2;TEXT_NODE=3
Example
<p> p </p><p> doughter </p> <p> son </p> <p> cousin </p>
For the p node, nodeType=1, nodeName="p" nodeValue =null
(nodeName saves the tag name)
var p = document.getElementById("p")
The attributes of node in P
##There is also a childNodes in P Properties, which are stored in the NodeList object. NodeList is an array-like object used to save an ordered set ofnodes, which can be accessed by location. Although the value of NodeList can be accessed through square bracket syntax, and this object also has a length property, it is not an instance of Array. The unique point of the NodeList object is that it is actually the result of dynamically executing a query based on the DOM structure, so changes in the DOM structure can be automatically reflected in the NodeList.
There is also an attribute called childrens for nodes. According to "Javascript Advanced Programming": Due to differences between other versions before IE9 and other browsers in handling whitespace characters in text nodes, the children attribute has appeared for a long time. This attribute is an instance of HTMLCollection and only contains child nodesthat are also elements in the element. Other than that, there is no difference between the two.
Based on the above example, let’s output both the childNodes and children of pp.childrenp.childNodesIt can be seen that1. The two belong to different instances, children is an instance of HTMLCollection, and childNodes is an instance of childNodes2 . children only contains child nodes that are also elements, and childNodes returns a collection of child elements of the element, including HTML nodes, attribute nodes, and text nodes. You can use nodeType to determine what type of node it is. It is an element node only when nodeType==1. (2=attribute node, 3=text node)The support for childNodes is as followsIE6-8/Safari/Chrome/Opera | IE9/Firefox | |
Supported | Not supported |