X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Ftraversing.js;h=42444c3f2261646b66b875e8c2ce634eb678f0c8;hb=d40083c866738727aa7ffd7f13d2955bc9575d5e;hp=57621a3906419e511e5bf415f9979324321333d9;hpb=39a77c2d02d379d15bfe9c2e7b90c92d9cb3a34e;p=jquery.git diff --git a/src/traversing.js b/src/traversing.js index 57621a3..42444c3 100644 --- a/src/traversing.js +++ b/src/traversing.js @@ -8,7 +8,7 @@ var runtil = /Until$/, var winnow = function( elements, qualifier, keep ) { if ( jQuery.isFunction( qualifier ) ) { return jQuery.grep(elements, function(elem, i) { - return !!qualifier.call( elem, i ) === keep; + return !!qualifier.call( elem, i, elem ) === keep; }); } else if ( qualifier.nodeType ) { @@ -57,6 +57,17 @@ jQuery.fn.extend({ return ret; }, + has: function( target ) { + var targets = jQuery( target ); + return this.filter(function() { + for ( var i = 0, l = targets.length; i < l; i++ ) { + if ( jQuery.contains( this, targets[i] ) ) { + return true; + } + } + }); + }, + not: function( selector ) { return this.pushStack( winnow(this, selector, false), "not", selector); }, @@ -64,6 +75,10 @@ jQuery.fn.extend({ filter: function( selector ) { return this.pushStack( winnow(this, selector, true), "filter", selector ); }, + + is: function( selector ) { + return !!selector && jQuery.filter( selector, this ).length > 0; + }, closest: function( selectors, context ) { if ( jQuery.isArray( selectors ) ) { @@ -109,6 +124,21 @@ jQuery.fn.extend({ return null; }); }, + + // Determine the position of an element within + // the matched set of elements + index: function( elem ) { + if ( !elem || typeof elem === "string" ) { + return jQuery.inArray( this[0], + // If it receives a string, the selector is used + // If it receives nothing, the siblings are used + elem ? jQuery( elem ) : this.parent().children() ); + } + // Locate the position of the desired element + return jQuery.inArray( + // If it receives a jQuery object, the first element is used + elem.jquery ? elem[0] : elem, this ); + }, add: function( selector, context ) { var set = typeof selector === "string" ? @@ -121,42 +151,13 @@ jQuery.fn.extend({ all ); }, - eq: function( i ) { - return i === -1 ? - this.slice( i ) : - this.slice( i, +i + 1 ); - }, - - first: function() { - return this.eq( 0 ); - }, - - last: function() { - return this.eq( -1 ); - }, - - slice: function() { - return this.pushStack( slice.apply( this, arguments ), - "slice", slice.call(arguments).join(",") ); - }, - - map: function( callback ) { - return this.pushStack( jQuery.map(this, function(elem, i){ - return callback.call( elem, i, elem ); - })); - }, - andSelf: function() { return this.add( this.prevObject ); - }, - - end: function() { - return this.prevObject || jQuery(null); } }); jQuery.each({ - parent: function(elem){return elem.parentNode;}, + parent: function(elem){var parent = elem.parentNode; return parent && parent.nodeType !== 11 ? parent : null;}, parents: function(elem){return jQuery.dir(elem,"parentNode");}, parentsUntil: function(elem,i,until){return jQuery.dir(elem,"parentNode",until);}, next: function(elem){return jQuery.nth(elem,2,"nextSibling");},