Differences between jsparentElement and offsetParent _ javascript skills

Source: Internet
Author: User
Here we mainly talk about the offsetParent attribute, which is not clearly explained in the MSDN document, which makes it harder to understand this attribute. I have found some information on the Internet over the past few days and added some tests of my own. I have a little understanding of this attribute and will summarize it here. The first is the parentElement attribute. This attribute is easy to understand, that is, the parent-child relationship defined in the DOM hierarchy. If element A contains Element B, Element B can obtain element A through the parentElement attribute.
Here we mainly talk about the offsetParent attribute, which is not clearly explained in the MSDN document, which makes it harder to understand this attribute. I have found some information on the Internet over the past few days and added some tests of my own. I have a little understanding of this attribute and will summarize it here.
To understand the offsetParent attribute, you must first specify the name of the "positioned element". The so-called "positioned element" refers to the style that sets the position attribute for the element, and the value of the position style attribute is equal to one of absolute, relative, and fixed.
When you use the offsetParent attribute to obtain a parent-level object, there are two situations:
1. The element has been located.
If the element itself has been located, the offsetParent attribute returns the parent element that has been located for this element. If no parent element has been located, the BODY object is returned, for example:

The Code is as follows:













Obj1.offsetParent returns the BODY object
Obj2.offsetParent returns the pObj1 object
2. The element is not located.
If the element is not located, offsetParent will not only find the parent element that has been located, but also find the parent element of the type TD and TABLE, if any of the three parent elements is found, this element is returned. Otherwise, the BODY object is returned, for example:

The Code is as follows:



















Obj1.offsetParent returns the td1 object
Obj2.offsetParent returns the pObj2 object
There is also the parentNode attribute in the attributes of the DOM element. In fact, this attribute is similar to the parentElement attribute. The parentElement attribute is unique to IE, And the W3C standard uses the parentNode attribute, children and childNodes are also exclusive to IE, and childNodes is supported by others.

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.