


"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:
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:
;
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:
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
< ;BODY>

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
