Home  >  Article  >  Web Front-end  >  仿jQuery的siblings效果的js代码_javascript技巧

仿jQuery的siblings效果的js代码_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:03:361239browse
复制代码 代码如下:

function siblings(o){//参数o就是想取谁的兄弟节点,就把那个元素传进去
var a=[];//定义一个数组,用来存o的兄弟元素
var p=o.previousSibling;
while(p){//先取o的哥哥们 判断有没有上一个哥哥元素,如果有则往下执行 p表示previousSibling
if(p.nodeType===1){
a.push(p);
}
p=p.previousSibling//最后把上一个节点赋给p
}
a.reverse()//把顺序反转一下 这样元素的顺序就是按先后的了
var n=o.nextSibling;//再取o的弟弟
while(n){//判断有没有下一个弟弟结点 n是nextSibling的意思
if(n.nodeType===1){
a.push(n);
}
n=n.nextSibling;
}
return a//最后按从老大到老小的顺序,把这一组元素返回
}
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