Added new parents, parent and siblings functions.

Siblings function still needs some work.
It would be nice to have an optional -or-self argument too.
This commit is contained in:
John Resig 2006-03-22 21:01:00 +00:00
parent 37394df837
commit 193866de0a

38
jquery/jquery.js vendored
View file

@ -212,21 +212,27 @@ function $(a,c) {
this.cur = this.old; this.cur = this.old;
return this; return this;
}, },
parent: function(a) { parent: function(a) {
if ( a == null ) a = 1; this.cur = $.map(this.cur,function(d){
this.cur = $.map(this.cur,function(d){ return d.parentNode;
var b = $.parents(d); });
if ( a == 0 ) if ( a ) this.cur = $.filter(a,this.cur).r;
return b; return this;
else if ( a.constructor == String ) { },
var c = $.filter(a,b);
return c.length > 0 ? c[0] : null; parents: function(a) {
} else this.cur = $.map(this.cur,$.parents);
return b.length >= a ? b[a-1] : null; if ( a ) this.cur = $.filter(a,this.cur).r;
}); return this;
return this; },
},
siblings: function(a) {
// Incorrect, need to exclude current element
this.cur = $.map(this.cur,$.sibling);
if ( a ) this.cur = $.filter(a,this.cur).r;
return this;
},
parents: function(a) { parents: function(a) {
return this; return this;
@ -573,7 +579,7 @@ $.filter = function(t,r,not) {
$.parents = function(a){ $.parents = function(a){
var b = []; var b = [];
var c = a.parentNode; var c = a.parentNode;
while ( c != null && c != c.documentElement ) { while ( c != null && c != document ) {
b[b.length] = c; b[b.length] = c;
c = c.parentNode; c = c.parentNode;
} }