Native JS node operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.