原生js实现jquery的siblings()

Element.prototype.siblings = function(callback){
            var siblingElement = [];
            var parentAllElement = [];
            if( ! this.parentNode ){
                return siblingElement;
            };
            parentAllElement = this.parentNode.getElementsByTagName(this.tagName);
            for( var i = 0; i < parentAllElement.length ; i++ ){
                if( parentAllElement[i] != this ){
                    siblingElement.push(parentAllElement[i]);
                    typeof callback == "function" && callback.call(parentAllElement[i]);
                }
            }
            return siblingElement;
        };

这样可以实现链式调用

 

posted @ 2019-05-30 14:29  evendemon  阅读(1334)  评论(2编辑  收藏  举报