JavaScript, I believe that we have tried to use getElementsByTagName and childnodes to achieve the traversal of the node. But getelementsbytagname to complex DOM structure traversal is obviously not as good as using childnodes, because ChildNodes
The values of childNodes. length and children. length are often different. Because
ChildNodes
Not only
Html
Node
, AllAttribute,TextAre included in
ChildNodes
Inside
,
You can use
NodeType
To determine the type of node, only when
NodeType = 1
Make
"ParentNode"Used to get the parent node of an element. Parentnodes is understood as a container with a child node in the container.Cases:my textIn the above code, you see the "Father" as a div container, the container has a "child", is the bold text
If you are a master, you are no stranger to childNodes and parentNode. In fact, they are two features/methods of DOM
If you are a master, you are no stranger to childNodes and parentNode. In fact, they are two features/methods of DOM.ChildNodes:
1, ChildNodes:Standard, returns a collection of child elements of the specified element, including HTML attributes, all attributes, and text. NodeType can be used to determine what type of node, only when Nodetype==1 is the element node, 2 is an
First of all to understand ParentNode ChildNodes is the standard.It's all about adding a Chrome browser to the HTML (please forgive my slag ...)For HTML documents, it can be understood that the node tree, the element tree, is made up of nodes in the
Take a simple HTML code as an Example
This is div01 This is div02 Childnodes: obtains all direct subnodes under a node.
VaR node = Document. getelementbyid ("Div"). childnodes; // obtain all subnodes under the DIV Node
At this time, if you alert
"ParentNode"
Used to get the parent node of an element. To understand parentnodes as a container with a child node in the container
Cases:my text
In the above code, you see "Dad" as a div container, the container has a "child", is the bold text
Parentelement obtains the parent object in the object hierarchy.
Parentnode obtains the parent object in the document level.
Childnodes obtains the set of HTML elements and textnode objects that are directly descendant of a specified object.
This article mainly introduces the differences between childNodes and children in JavaScript Operations on DOM elements. This article describes the differences between the test code and the running effect. if you need them, refer to DOM elements,
element. ChildNodes: read-only attribute child node list collectionUnder standard browser: Contains text and element type nodes, and also contains illegal nested child nodesNon-standard browser: Contains only element type nodes, IE7 does not contain
Using childNodes in JS to get the child element line feed will generate a child element Such code$ ('# Div1'). childNodes. length => it will be equal to 5.If you don't wrap the code, you will get 2$ ('# Div1'). childNodes. length 5 $ ('# div1').
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
A bug that Firefox handles to ChildNodes
Childnodesfirefox No filter wrapping and spaces in the processing childnodes. So in the initial use, the results are not expected effect.
Html
Copy Code code as follows:
Finance University
Parentelement gets the parent object in the object hierarchy.ParentNode gets the parent object in the document hierarchy.ChildNodes gets a collection of HTML elements and Textnode objects that are direct descendants of the specified object.Children
This article mainly introduces the differences between childNodes and children in JavaScript operations on DOM elements. This article describes the differences between the test code and the running effect. If you need them, refer to DOM elements,
Problem Description: Move the four nodes below to the right one at a single point or move the node to the right at once to the left (>> move right,
The beginning of the implementation is this:
MyHtml.html -->
JS in the childnodes to get the child elements to change the line to produce a child element
Such a code
$ (' #div1 ') .childnodes.length==> will be equal to 5 OH
If you don't change the code, you're going to get 2.
$ (' #
Properties Introduction
ChildrenChildren: Returns a collection of all the immediate child nodes of the parent element, noting that children only returns HTML element nodes, excluding text nodes and attribute nodes.
ChildNodeschildnodes: Returns a
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.