原生js实现jquery的siblings()

时间:2021-12-13 07:23:16
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;
};

这样可以实现链式调用