原生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);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章