原生js获取兄弟元素

//获取某元素后相邻的所有class名为MsoToc2的兄弟元素
nextAll(node){ let that
= this, nextNode = that.next(node), nodeArray = []; while(nextNode != null && that.hasClass(nextNode,'MsoToc2')){ nodeArray.push(nextNode); nextNode = that.next(nextNode); } return nodeArray; }
如:
this.nextAll(msoToc1[i])获取元素msoToc1[i]后边相邻的所有class名为MsoToc2的元素


    //下一个兄弟节点
    next = (node) => {
        let next = node.nextSibling;
        if(next !== null && next.nodeType === 3 ){ //防止内联元素在ie下出现的空白节点和火狐下的空白节点
            return next.nextSibling;
        }
        return next;
    }
    hasClass = (element, cName) => {
        let reg = new RegExp("(?:^| +)" + cName + "(?: +|$)", "g");
        return reg.test(element.className);
    }

 

posted @ 2020-12-13 18:11  dongxiaolei  阅读(3850)  评论(0编辑  收藏  举报