The difference between parentnodes, childNodes and children

Source: Internet
Author: User

"ParentNode"

Used to get the parent node of an element. Parentnodes is understood as a container with a child node in the container.

Cases:
<div id= "Parent" >
<b id= "Child" >my text</b>
</div>

In the above code, you see the "Father" as a div container, the container has a "child", is the bold text part. If you are going to use the getElementById () method to get the bold element and want to know who it "Dad" is, the information returned will be a Div. show the script below and you'll know what's going on ...

"Children"

There are times when you find that calling ChildNodes belongs without getting the value you expect. Like what:

Html:

<a href= "javascript:void (0);" onclick= "selectcategory (' 1,this);" >
<span>apple</span>
</a>

Js:

function Selectcategory (val,obj) {
var text = obj.children[0].innerhtml;

}

The value we want to get for the Text property is "Apple", but the actual value is undefined, which means that the fetch failed.

Reason:

The ChildNodes property return value contains the element node and the text node, in this case obj.children[0] is actually a space returned.

Solution:

1) Remove the space between <span> and <a>.

2) switch to obj.children[0].innerhtml. The Children property returns only the element nodes.

"ChildNodes"

That returns a collection of child elements of the specified element, including the HTML node, all attributes, and text.

Example:

function SetCount (count) {
var counts = Count.split (",");
for (var i = 0; i < counts.length; i++) {
var j = i + 1;
document.getElementById (' Text ' + j). InnerText = Counts[i];
}
}
function settable () {
var table = document.getElementById ("BODYTB");
var n = 1
for (var i = 2; I <= 7; i++) {
var row = table.rows (i);
if (i = = 3) {
for (var j = 2; J <=, J + +) {
Row.insertcell (j). InnerHTML = "<span style= ' width:100% ' id= ' Text" + n + "' ondblclick= ' excdwe (this) ' ></span > ";
n++;
}
}
else {
for (var j = 1; J <=; J + +) {
Row.insertcell (j). InnerHTML = "<span style= ' width:100% ' id= ' Text" + n + "' ondblclick= ' excdwe (this) ' ></span > ";
n++;
}
}
}
}

The difference between parentnodes, childNodes and children

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.