Home Web Front-end JS Tutorial Detailed explanation of the application of parentNode, childNodes, and children in javascript_javascript skills

Detailed explanation of the application of parentNode, childNodes, and children in javascript_javascript skills

May 16, 2016 pm 05:08 PM
children parentnode

"parentNode"

is often used to get the parent node of an element. Understand parentNodes as a container, and there are child nodes in the container.

Example:


My text

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 If you want to know who its "father" is, the returned information will be a div. Demonstrate the following script and you will know what is going on...

Quote:

Copy code The code is as follows:


My text



Using parentNode does not necessarily only Find a "father", "son" can also become "father", as in the following example...

Quote:

Copy code The code is as follows:


                                                                                  ;


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 code The code is as follows:
                                                                                    ;



Did you notice that two parentNodes are used together? "parentNode.parentNode". The first parentNode is a div (id "childparent"). Because we want to get the outermost parent element, we add another parentNode and there it is. div (id "parent").
Use parentNode to not only find the nodeName of an element, but also more. For example, you can get the parent node that contains a large number of elements and add a new node at the end.
IE has its own name called "parentElement", for cross-browser scripts it is recommended to use parentNode.

A few 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

And IE is:

Object Required

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 , call the function after the document is loaded. For example, add it to the Body tag.

What is the difference 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, 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

The first method

Copy the code The code is as follows:




New Document </ TITLE><br><META NAME="Generator" C><br><META NAME="Author" C><br><META NAME="Keywords" C><br><META NAME=" Description" C><br><SCRIPT LANGUAGE="JavaScript"><br><!--<BR>var row = -1;<BR>function showEdit(obj){<BR>var cell2 = obj .parentNode.parentNode.childNodes[1];<BR>var rowIndex = obj.parentNode.parentNode.rowIndex;<BR>cell2.innerHTML = "<input type='text' value='" cell2.innerHTML "'> ;";<BR>if(row != -1){<BR>var oldCell2 = document.getElementById("tb").rows[row].cells[1];<BR>oldCell2.innerHTML = oldCell2.childNodes [0].value;<BR>}<BR>row = rowIndex;<BR>}<BR>//--><br></SCRIPT><br></HEAD><br>< ;BODY><br><TABLE id="tb"><br><TR><br><TD><input type="radio" name="rad"></TD> <br><TD></TD><br><TD></TD><br></TR><br><TR><br><TD><input type ="radio" name="rad"></TD><br><TD></TD><br><TD></TD><br></TR><br><TR><br><TD><input type="radio" name="rad"></TD><br><TD></TD><br><TD> ;</TD><br></TR><br></TABLE><br></BODY><br></HTML><br> </div> <br><strong> The second method<br></strong><div class="codetitle"> <span><a style="CURSOR: pointer" data="79486" class="copybut" id="copybut79486" onclick="doCopy('code79486')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code79486"> <br><table border=1 width=100%><br><tr><br>    <td><input name=m type=checkbox ></td><br>    <td>1111</td><br>    <td><input name=aaa value="222" disabled></td><br>    <td><input name=bbb value="333" disabled></td><br></tr><br><tr><br>    <td><input name=m type=checkbox ></td><br>    <td>1111</td><br>    <td><input name=aaa value="222" disabled></td><br>    <td><input name=bbb value="333" disabled></td><br></tr><br><tr><br>    <td><input name=m type=checkbox ></td><br>    <td>1111</td><br>    <td><input name=aaa value="222" disabled></td><br>    <td><input name=bbb value="333" disabled></td><br></tr><br></table><br><SCRIPT LANGUAGE="JavaScript"><br>function mm(e)<br>{<br>var currentTr=e.parentElement.parentElement;<br>var inputObjs=currentTr.getElementsByTagName("input");<br>for(var i=0;i<inputObjs.length;i )<br>{<br>   if(inputObjs[i ]==e) continue;<br>    inputObjs[i ].disabled=!e.checked;<br>}<br>}<br></SCRIPT><br> </div> <br>获取HTML中的父控件方法<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="61733" class="copybut" id="copybut61733" onclick="doCopy('code61733')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code61733"> <br>function setvalue(v,o)<br>    { <br>        //var obj=document.getElementById(''batchRate'');<br>        //windows.<br>        alert(o.parentNode.innerHTML); <p>        alert(o.parentNode); //parentNode此处也是获取父控件</p> <p>        alert(o.parentElement); //parentElement此处也是获取父控件</p> <p>        alert(o.parentElement.parentNode); //parentElement.parentNode此处也是获取父控件</p> <p>        //o.parentNode.bgColor="red";<br><br>         o.parentElement.parentNode.bgColor="red";<br>    }<br></p> </div> <br>实例:<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="88436" class="copybut" id="copybut88436" onclick="doCopy('code88436')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code88436"> <br><html><br><head><br><meta http-equiv="Content-Language" c><br><meta http-equiv="Content-Type" c><br><title>新建网页 1

<script><br>    function setvalue(v,o)<br>    { <br>        //var obj=document.getElementById(''batchRate'');<br>        //windows.<br>        alert(o.parentNode.innerHTML); <p>        alert(o.parentNode);</p> <p>        alert(o.parentElement);</p> <p>        //o.parentNode.bgColor="red";<br><br>       o.parentElement.parentNode.bgColor="red";<br>    }<br></script>







Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles
dfsdfdsfdsa