OffsetParent refers to position-related ancestor elements read-only
ParentNode refers to position-independent ancestor elements read-only
function OffsetTop (Element) {var top=element.offsettop;var parent=element.offsetparent;while (parent!=null) {top+= Parent.offsettop;parent=parent.offsetparent;} return top;}
This is to get the element to the top of the position where offsetparent can be replaced by parentnode?
I didn't try it, why not?
Offsetparent tested a bit to get to the body element, and to get it is null
Console.log (document.getElementById (' t '). offsetParent); /* If the entire page is a id=t div gets the body, from the body upward is null*/
ParentNode can fetch up to document object
Console.log (Document.documentElement.parentNode); #document is also null to get up
Do you think it's all null at first? Why not? But the return value is Nan
Originally
Console.log (DOCUMENT.BODY.OFFSETTOP); /* Output is 0,document.body represents body*/
Look again
Console.log (DOCUMENT.BODY.PARENTNODE.OFFSETTOP);/* Output 0, Document.body.parentNode = html*/
Console.log (DOCUMENT.BODY.PARENTNODE.PARENTNODE.OFFSETTOP); /* Output Undefined, Document.body.parentNode.parentNode represents the Document Object */
Because
Document.body.parentNode.parentNode not equal to NULL
So it is still possible to enter this while loop undefined+ a digital =nan;
Therefore, it is not possible to replace offsetparent with ParentNode, so the offsettop reference element should be the Document object
Summary: offsetparent can get up to body, and then fetch is NULL
ParentNode can fetch the document object up to and get it again is null
The top-level element that can use the offsettop property is HTML, and if you use offsettop to return undefined with the upper-level document of HTML, the reference element that offsettop gets the distance should be the Document object
Write to this recall definition of the offsettop () function, is to change in while the condition is also ok ha, but much trouble
The difference between parentnode and offsetparent