The difference between parentnode and offsetparent

Source: Internet
Author: User

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

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.