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:
- the value of nodetype for element nodes is 1
- the value of nodetype of the attribute node is 2
- the value of nodetype for a text node is 3