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:
CopyCode The Code is as follows: <body>
<P>
<Div>
<Span id = "obj1" style = "position: absolute"> </span>
</Div>
<Div id = "pobj1" style = "position: absolute">
<Span id = "obj2" style = "position: absolute"> </span>
</Div>
</P>
</Body>
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:Copy codeThe Code is as follows: <Table width = "500" border = "0">
<Tr>
<TD id = "TD1">
<Div id = "pobj1">
<Span id = "obj1"> </span>
</Div>
</TD>
</Tr>
<Tr>
<TD>
<Div id = "pobj2" style = "position: relative">
<Span id = "obj2"> </span>
</Div>
</TD>
</Tr>
</Table>
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.