4. childNodes attribute: Returns an array composed of child nodes of element nodes.
Since text nodes and attribute nodes cannot contain any child nodes,
So their childNodes property returns an empty array forever.
You can use the hasChildNodes method, which is used to determine whether an element has child nodes.
or if ( < 1);
childNodes is also a read-only property. If you want to add nodes, you can use appendChild() or insertBefore(),
To delete a node, you can use removeChild();
After the operation, the childNodes property will be refreshed automatically.
5, firstChild attribute:
Since text nodes and attribute nodes cannot contain any child nodes,
So their firstChild property always returns an empty array. If there are no children, null will be returned;
Equivalent to [0] ;
The firstChild property is a read-only property.
6, lastChild attribute:
Since text nodes and attribute nodes cannot contain any child nodes,
So their lastChild property always returns an empty array. If there are no children, null will be returned;
Equivalent to [ - 1 ] ;
The lastChild property is a read-only property.
7 , nextSibling properties:
Returns the next sibling node of the target node.
If there is no node behind the target node that belongs to the same parent node, nextSibling will return null;
nextSibling attribute is a read-only attribute.
8 , previousSibling attribute:
Returns the previous sibling node of the target node.
If there is no node in front of the target node that belongs to the same parent node, previousSibling will return null;
The previousSibling property is a read-only property.
9, parentNode attribute:
Note: The node returned by the parentNode attribute is always an element node, because only the element node can have child nodes.
Of course there is an exception:
document node, it has no parent node. So the parentNode property of the document node will return null;
The parentNode property is a read-only property.
OK, let’s talk about the common properties and methods of DOM here, let’s understand the use of these methods.
I believe that everyone's DOM programming technology will be greatly improved.
If you still don’t understand, you can search for information on Google.
Previous page12Read the full text