Book: "Pro JavaScript techniques proficient in JavaScript" simple Dom Traversal

Source: Internet
Author: User

// Find the function of the previous sibling element of the relevant element

Function Prev (ELEM ){

Do {

ELEM = ELEM. previussibling;

} While (ELEM & ELEM. nodetype! = 1 );

Return ELEM;

}

// Find the function of the first child element of an element

Function first (ELEM ){

ELEM = ELEM. firstchild;

Return ELEM & ELEM. nodetype! = 1?

Next (ELEM): ELEM;

}

// Find the function of the last child element of an element

Function last (ELEM ){

ELEM = ELEM. lastchild;

Return ELEM & ELEM. nodetype! = 1?

Prev (ELEM): ELEM;

}

// Find the function of the element parent Element

Function parent (ELEM; num ){

Num = num | 1;

For (VAR I = 0; I <num; I ++)

If (ELEM! = NULL) ELEM = ELEM. parentnode;

Return ELEM;

}

With these new functions, you can quickly traverse Dom documents without worrying about text between elements. For example, you need to find the next element of the

// Search for the next element of the

Next (first (document. Body ))

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.