Get child nodes
1. Children is not a standard DOM attribute, but is supported by almost all browsers. Gets the element node of the child element (including element nodes only)
Note: In IE, children contains the comment node.
2. ChildNodes is a standard attribute. Returns all child nodes. Include text nodes.
Attention:
(1) NodeType put back the value of the node type, which is generally an integer
Commonly used are: Nodetype==1 is an element node, 2 is an attribute node (attr), 3 is a text node (#text), 8 is a comment node (#comment), and 9 is a document.
(2) The name of the NodeName tag, the returned result is all uppercase
(3) NodeValue returns value, but is useful only for text nodes and annotations, and does not work for labels.
<ulID= "UL1"> <Li> <span>I am the text</span> </Li> <Li></Li> <Li></Li></ul>
window.onload=function() {
var oul1=document.getelementbyid ("UL1");
Console.log (// 3 // 7 for (var i=0; i<oul1.children.length; i++) { oul1.children[i].style.background= ' Red ' ; }}
ChildNodes Analog Children
functionMychildren (ele) {//gets all the child elements (including text nodes, element nodes, annotation nodes); varElechild =Ele.childnodes; varstr = []; //Loop through all the text nodes so that all can be obtained; for(vari = 0;i<elechild.length;i++){ //NodeType = = 1 is an element node, 2 is an attribute node (attr), 3 is a text node (text), 8 is a comment node, and 9 is a document. if(Elechild[i].nodetype = = 1){ //add each of the resulting child elements to the back of the array;Str.push (Elechild[i]); } } returnstr;}//ChildNodes Analog ChildrenfunctionMyChildren2 (ele) {//gets all the child elements (including text nodes, element nodes, annotation nodes); varElechild =Ele.childnodes; //Loop through all the text nodes so that all can be obtained; for(vari = 0;i<elechild.length;i++){ //Console.log (elechild[i].nodename); #text Li if(Elechild[i].nodename = = "#text" &&!/\s/. Test (Elechild[i].nodevalue)) { //Delete the text in the arrayEle.removechild (Elechild[i]); } } returnElechild;}
Source Address: Https://github.com/zuobaiquan/javascript/tree/master/%E5%8E%9F%E7%94%9FJS%E8%8A%82%E7%82%B9%E6%93%8D%E4%BD%9C
If you think the article is good, please Bo Master drink a cup of tea Oh!!!
Native JS node operation