Home > Web Front-end > JS Tutorial > js code that imitates jQuery's siblings effect_javascript skills

js code that imitates jQuery's siblings effect_javascript skills

WBOY
Release: 2016-05-16 18:03:36
Original
1360 people have browsed it
Copy code The code is as follows:

function siblings(o){//Parameter o is who you want to take For sibling nodes, pass that element in
var a=[]; //Define an array to store the sibling elements of o
var p=o.previousSibling;
while(p){/ /First take the brothers of o to determine whether there is a previous brother element. If there is, proceed to p to represent previousSibling
if(p.nodeType===1){
a.push(p);
}
p=p.previousSibling//Finally assign the previous node to p
}
a.reverse()//Reverse the order so that the order of the elements is sequential
var n=o.nextSibling;//Get o’s younger brother
while(n){//Determine whether there is a next younger brother. Node n means nextSibling
if(n.nodeType===1) {
a.push(n);
}
n=n.nextSibling;
}
return a//Finally, put this group of elements in order from oldest to youngest Return
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template