Home  >  Article  >  Web Front-end  >  js code that imitates jQuery’s siblings effect_javascript skills

js code that imitates jQuery’s siblings effect_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:03:361299browse
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
}
Statement:
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