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

Source: Internet
Author: User

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>

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>

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.

By the above, do you understand?

Category: JavaScript

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.