"parentNode"
Often used to get the parent node of an element. Think of parentNodes as a container, and there are child nodes in the container.
example:
<div id="parent">
<b id="child">My text</b>
</div>
In the above code, you see that "Dad" is used as a div container, and there is a "child" in the container, which is the bold text part. If you plan to use the getElementById() method to get the bold element and want to know it Who is "Dad"? The returned information will be a div. Demonstrate the following script and you will know what is going on...
Quote:
Copy the code code as follows:
<div id="parent">
<b id="child">My text</b>
</div>
<script type="text/javascript">
<!--
alert(document.getElementById("child").parentNode.nodeName);
//-->
</script>
Using parentNode does not necessarily only find a "father", a "son" can also become a "father", as in the following example...
Quote:
Copy the code code as follows:
<div id="parent">
<div id="childparent">
<b id="child">My text</b>
</div>
</div>
There are two "parents" and two "children" in the above code. The first div (id "parent") is the "father" of the second div (childparent).
There is a bold element (id "child") in "childparent", which is the "child" of the "childparent" div. So, how to access "grandpa" (id "parent")? It's very simple....
Quote:
Copy the code code as follows:
<div id="parent">
<div id="childparent">
<b id="child">My text</b>
</div>
</div>
<script type="text/javascript">
<!--
alert(document.getElementById("child").parentNode.parentNode.nodeName);
//-->
</script>
Did you notice that two parentNodes are used together? "parentNode.parentNode". The first parentNode is div (id "childparent"), because we want to get the outermost parent element, so we add another parentNode to div (id "childparent") "parent").
Using parentNode does more than just find the nodeName of an element. For example, you can get the parent node of a large number of elements and add a new node at the end.
IE has its own name called "parentElement", and for cross-browser scripts it is recommended to use parentNode.
Two more words:
If you put javascript at the head of the html file, an error will occur. Firefox will report the following error:
document.getElementById("child") has no properties
For IE it is:
ObjectRequired
The reason is that all browsers that support JavaScript run JavaScript before fully parsing the DOM. In actual Web programming, most JavaScript may be placed in the head tag. In order to run properly, alert needs to be wrapped in the function and in the document Call the function after loading. For example, add it to the Body tag.
What are the differences between parentNode, parentElement, childNodes, and children?
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 Gets a collection of DHTML objects that are direct descendants of the object.
-------------------------------------------------- ------
parentNode has the same function as parentElement, and childNodes has the same function as children. However, parentNode and childNodes comply with W3C standards and can be said to be relatively universal. The other two are only supported by IE, not standards, and are not supported by Firefox.
-------------------------------------------------- ------
In other words, parentElement and children are IE's own things and are not recognized by other places.
Then, their standard version is parentNode, childNodes.
The functions of these two are the same as parentElement and children, and they are standard and universal.
-------------------------------------------------- ------
The following is a simple explanation, please pay attention to the differences in individual words:
parentNode Property: Retrieves the parent object in the document hierarchy.
parentElement Property:Retrieves the parent object in the object hierarchy.
childNodes:
Retrieves a collection of HTML Elements and TextNode objects that are direct descendants of the specified object.
children:
Retrieves a collection of DHTML Objects that are direct descendants of the object.
parentElement parentNode.parentNode.childNodes usage example
first method
Copy the code code as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" C>
<META NAME="Author" C>
<META NAME="Keywords" C>
<META NAME="Description" C>
<SCRIPT LANGUAGE="JavaScript">
<!--
var row = -1;
function showEdit(obj){
var cell2 = obj.parentNode.parentNode.childNodes[1];
var rowIndex = obj.parentNode.parentNode.rowIndex;
cell2.innerHTML = "<input type='text' value='"+ cell2.innerHTML +"'>";
if(row != -1){
var oldCell2 = document.getElementById("tb").rows[row].cells[1];
oldCell2.innerHTML = oldCell2.childNodes[0].value;
}
row = rowIndex;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<TABLE id="tb">
<TR>
<TD><input type="radio" name="rad"></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD><input type="radio" name="rad"></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD><input type="radio" name="rad"></TD>
<TD></TD>
<TD></TD>
</TR>
</TABLE>
</BODY>
</HTML>
Second method
Copy the code code as follows:
<table border=1 width=100%>
<tr>
<td><input name=m type=checkbox ></td>
<td>1111</td>
<td><input name=aaa value="222" disabled></td>
<td><input name=bbb value="333" disabled></td>
</tr>
<tr>
<td><input name=m type=checkbox ></td>
<td>1111</td>
<td><input name=aaa value="222" disabled></td>
<td><input name=bbb value="333" disabled></td>
</tr>
<tr>
<td><input name=m type=checkbox ></td>
<td>1111</td>
<td><input name=aaa value="222" disabled></td>
<td><input name=bbb value="333" disabled></td>
</tr>
</table>
<SCRIPT LANGUAGE="JavaScript">
function mm(e)
{
var currentTr=e.parentElement.parentElement;
var inputObjs=currentTr.getElementsByTagName("input");
for(var i=0;i<inputObjs.length;i++)
{
if(inputObjs[i ]==e) continue;
inputObjs[i ].disabled=!e.checked;
}
}
</SCRIPT>
Get the parent control method in HTML
Copy the code code as follows:
function setvalue(v,o)
{
//var obj=document.getElementById(''batchRate'');
//windows.
alert(o.parentNode.innerHTML);
alert(o.parentNode); //parentNode also gets the parent control here
alert(o.parentElement); //parentElement also gets the parent control here
alert(o.parentElement.parentNode); //parentElement.parentNode also gets the parent control here
//o.parentNode.bgColor="red";
o.parentElement.parentNode.bgColor="red";
}
Example:
Copy the code code as follows:
<html>
<head>
<meta http-equiv="Content-Language" c>
<meta http-equiv="Content-Type" c>
<title>New web page 1</title>
</head>
<script>
function setvalue(v,o)
{
//var obj=document.getElementById(''batchRate'');
//windows.
alert(o.parentNode.innerHTML);
alert(o.parentNode);
alert(o.parentElement);
//o.parentNode.bgColor="red";
o.parentElement.parentNode.bgColor="red";
}
</script>
<body>
<table id="table1">
<tr>
<td><a >dfsdfdsfdsa</a></td>
<td> </td>
<td> </td>
</tr>