The difference between element and node in JavaScript, the difference between children and childnodes

Source: Internet
Author: User

This article starting blog garden: http://jscode.cnblogs.com, reprint please indicate the source.

The difference between element and node, Cilldren and childnodes Many friends do not know, this article tries to let you understand the differences between the concepts.

node is a common name for any type of object in the DOM hierarchy, and node has many types, such as element nodes, attribute nodes, text nodes, annotation nodes, etc., which are distinguished by nodetype, and are common:

node Type NodeType
Elemental Element 1
Property attr 2
Textual text 3
Comment Comments 8
Document documents 9

More node types reference: https://developer.mozilla.org/en-US/docs/DOM/Node.nodeType?redirectlocale=en-US&redirectslug= NodeType

element inherits the node class, which means that element is one of several types of node , that is, when NodeType is 1 o'clock, node is Elementnode, and element expands node,element ID, class, children, and other attributes.

These are the differences between element and node.

So what do you take with document.getElementById ("xxx") for node or element? Let's test it out:

<!DOCTYPE HTML><HTML><Head>    <MetaCharSet= "Utf-8">    <title>Demo</title></Head><Body>    <DivID= "Test">        <P>One</P>        <P>Both</P>    </Div>    <Script>        varOdiv=document.getElementById ("Test"); Console.log (OdivinstanceofNode); //trueConsole.log (OdivinstanceofElement); //true    </Script></Body></HTML>

We can see that document.getElementById ("xxx") takes the element as well as node.

  Children is the attribute of element, and ChildNodes is the attribute of node , let's Test it again:

<!DOCTYPE HTML><HTML><Head>    <MetaCharSet= "Utf-8">    <title>Demo</title></Head><Body>    <DivID= "Test">        <P>One</P>        <P>Both</P>    </Div>    <Script>        varOdiv=document.getElementById ("Test"); Console.log (odiv.children[0] instanceofNode); //trueConsole.log (odiv.children[0] instanceofElement); //trueConsole.log (odiv.childnodes[0] instanceofNode); //trueConsole.log (odiv.childnodes[0] instanceofElement); //falseConsole.log (typeofodiv.childnodes[0].children); //undefinedConsole.log (typeofodiv.childnodes[0].childnodes); //Object    </Script></Body></HTML>

As we can see from the above code, element children[0] is still element, which is an instance of node and element, node's childndoes[0] is node, just an instance of node, not an instance of element.

At the same time,node's Children property is undefined.

When I take a child element with children[0] Instead, you can use the corresponding method Getboundingclientrect () to get the position of the element

It is sometimes obtained when using childnodes to get [object htmldlistelement], but occasionally it gets [object Text].

Some people say that it is a browser problem, but I use the Firefox test, and then I found that [object Text] is because I changed the line childnodes gave me a space when the first node, remove the line is OK

The difference between element and node in JavaScript, the difference between children and childnodes

Related Article

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.