// -> prev: 获取上一个哥哥元素节点
// -> 首先获取当前元素的上一个哥哥元素,判断是否为元素节点,不是的话基于当前的继续找到上面的哥哥节点..
// 直到找到哥哥元素节点为止,如果没有哥哥元素节点,则返回null
function prev(curEle) {
if (flag) {
return curEle.previousElementSibling;
}
var pre = curEle.previousSibling;
while (pre && pre.nodeType !== 1) {
pre = pre.previousSibling;
}
return pre;
}
// -> next: 获取下一个弟弟元素节点
function next(curEle) {
if (flag) {
return curEle.nextElementSibling;
}
var nex = curEle.nextSibling;
while (nex && nex.nodeType !== 1) {
nex = nex.nextSibling;
}
return nex;
}
// -> prevAll: 获取所有的哥哥元素节点
function prevAll(curEle) {
var ary = [];
var pre = this.prev(curEle);
while (pre) {
ary.unshift(pre);
pre = this.prev(pre);
}
return ary;
}
// -> nextAll: 获取所有的弟弟节点
function nextAll(curEle) {
var ary = [];
var nex = this.next(curEle);
while (nex) {
ary.push(nex);
nex = this.next(nex);
}
return ary;
}
// -> sibling: 获取相邻的两个元素节点
function sibling(curEle) {
var pre = this.prev(curEle);
var nex = this.next(curEle);
var ary = [];
pre ? ary.push(pre) : null;
nex ? ary.push(nex) : null;
return ary;
}
// -> siblings: 获取所有的兄弟元素节点
function siblings(curEle) {
return this.prevAll(curEle).concat(this.nextAll(curEle));
}
// -> index: 获取当前元素的索引
function index(curElem) {
return this.prevAll(curElem).length;
}
// -> firstChild: 获取第一个元素子节点
function firstChild(curElem) {
var childrenNodes = this.children(curElem);
return childrenNodes.length > 0 ? childrenNodes[0] : null;
}
// -> lastChild: 获取最后一个元素子节点
function lastChild(curElem) {
var childrenNodes = this.children(curElem);
return childrenNodes.length > 0 ? childrenNodes[childrenNodes.length - 1] : null;
}
网友评论