search
HomeWeb Front-endJS TutorialDetailed explanation of the application of parentNode, childNodes, and children in javascript_javascript skills

"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

alert(document.getElementById("child").parentNode.nodeName);
//-->


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><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><br></td> </tr> <br> </table> <br><br>
Statement
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
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

jQuery Check if Date is ValidjQuery Check if Date is ValidMar 01, 2025 am 08:51 AM

Simple JavaScript functions are used to check if a date is valid. function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] '/' bits[1] '/' bits[0]); return !!(d && (d.getMonth() 1) == bits[1] && d.getDate() == Number(bits[0])); } //test var

jQuery get element padding/marginjQuery get element padding/marginMar 01, 2025 am 08:53 AM

This article discusses how to use jQuery to obtain and set the inner margin and margin values ​​of DOM elements, especially the specific locations of the outer margin and inner margins of the element. While it is possible to set the inner and outer margins of an element using CSS, getting accurate values ​​can be tricky. // set up $("div.header").css("margin","10px"); $("div.header").css("padding","10px"); You might think this code is

10 jQuery Accordions Tabs10 jQuery Accordions TabsMar 01, 2025 am 01:34 AM

This article explores ten exceptional jQuery tabs and accordions. The key difference between tabs and accordions lies in how their content panels are displayed and hidden. Let's delve into these ten examples. Related articles: 10 jQuery Tab Plugins

10 Worth Checking Out jQuery Plugins10 Worth Checking Out jQuery PluginsMar 01, 2025 am 01:29 AM

Discover ten exceptional jQuery plugins to elevate your website's dynamism and visual appeal! This curated collection offers diverse functionalities, from image animation to interactive galleries. Let's explore these powerful tools: Related Posts: 1

HTTP Debugging with Node and http-consoleHTTP Debugging with Node and http-consoleMar 01, 2025 am 01:37 AM

http-console is a Node module that gives you a command-line interface for executing HTTP commands. It’s great for debugging and seeing exactly what is going on with your HTTP requests, regardless of whether they’re made against a web server, web serv

Custom Google Search API Setup TutorialCustom Google Search API Setup TutorialMar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

jquery add scrollbar to divjquery add scrollbar to divMar 01, 2025 am 01:30 AM

The following jQuery code snippet can be used to add scrollbars when the div content exceeds the container element area. (No demonstration, please copy it directly to Firebug) //D = document //W = window //$ = jQuery var contentArea = $(this), wintop = contentArea.scrollTop(), docheight = $(D).height(), winheight = $(W).height(), divheight = $('#c

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools