On the parent node of JS HTML element and the _javascript technique of the child node

Source: Internet
Author: User

Like ParentNode and parentelement functions,childnodes and children function as well. But parentnode and childnodes are consistent with the standard of the Global Consortium, which can be said to be more generic. While the other two are only IE support, not the standard, Firefox does not support

Example:

"ParentNode" is often used to get the parent node of an element. The Parentnodes is understood as a container with a child node in the container, as follows:

<div id= "Parent" >
<b id= "Child" >my text</b>
</div>

In the above code, you see "Dad" as a div container, the container has a "child", is the bold text part. If you're going to use the getElementById () method to get a bold element and want to know who the "Daddy" is, the information returned will be a Div. The following script is shown:

<script type= "Text/javascript" >
<!--

alert (document.getElementById "Child"). Parentnode.nodename );

-->
</script>

With ParentNode not only find a "dad", "son" can also become "Dad", as the following example ...

<div id= "Parent" >
<div id= "childparent" >
<b id= "Child" >my text</b>
</div >
</div>

There are two "daddies" and two "children" in this 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 "kid" of the "childparent" div. So, how to access to "Grandpa" (id "parent")? as follows:

<script type= "Text/javascript" >
<!--

alert (document.getElementById ("Child"). ParentNode.parentNode.nodeName);


-->
</script>

Did you notice that two parentnode were used? "Parentnode.parentnode". The first parentnode is the div (id "childparent"), because we want to get the outermost parent element, so another parentnode is added to the DIV (id "parent").

Using ParentNode not only finds an element's nodename, but 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", which is recommended for cross browser scripting ParentNode

The above is a simple discussion of the HTML elements of the parent node, the child node is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.