Nextsibling and lastsibling

Source: Internet
Author: User

There are many spaces in Firefox as text nodes, so problems will occur when we use nextsibling and previoussibling. Because Firefox will mistakenly treat the text node as the sibling node of the element node. We can add nodetype to judge. When the last or next node is a text node, continue searching until the next element node is found. The following code is for reference only and passes the test in Firefox:

// Function nextsibling (node) {var templast = node. parentnode. lastchild; If (node = templast) return NULL; var tempobj = node. nextsibling; while (tempobj. nodetype! = 1 & tempobj. nextsibling! = NULL) {tempobj = tempobj. nextsibling;} return (tempobj. nodetype = 1 )? Tempobj: NULL;} // function prevsibling (node) {var tempfirst = node. parentnode. firstchild; If (node = tempfirst) return NULL; var tempobj = node. previussibling; while (tempobj. nodetype! = 1 & tempobj. previussibling! = NULL) {tempobj = tempobj. previussibling;} return (tempobj. nodetype = 1 )? Tempobj: NULL ;}
Test code

Among them, the value of nodetype mainly includes the following types:

    1. the value of nodetype for element nodes is 1
    2. the value of nodetype of the attribute node is 2
    3. the value of nodetype for a text node is 3

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.