"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:
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:
Quote:
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
1111 | |||
1111 | |||
1111 |
alert(o.parentNode); //parentNode此处也是获取父控件
alert(o.parentElement); //parentElement此处也是获取父控件
alert(o.parentElement.parentNode); //parentElement.parentNode此处也是获取父控件
//o.parentNode.bgColor="red";
o.parentElement.parentNode.bgColor="red";
}
dfsdfdsfdsa |