Add node and jQuery object support to $.fn.closest
This commit is contained in:
parent
eb1497c1fb
commit
e09d8898d8
|
@ -32,6 +32,7 @@ jQuery.fn.extend({
|
|||
|
||||
var ret = this.pushStack( "", "find", selector ),
|
||||
length, n, r;
|
||||
|
||||
for ( i = 0, l = this.length; i < l; i++ ) {
|
||||
length = ret.length;
|
||||
jQuery.find( selector, this[i], ret );
|
||||
|
@ -78,6 +79,7 @@ jQuery.fn.extend({
|
|||
closest: function( selectors, context ) {
|
||||
var ret = [], i, l, cur = this[0];
|
||||
|
||||
// Array
|
||||
if ( jQuery.isArray( selectors ) ) {
|
||||
var match, selector,
|
||||
matches = {},
|
||||
|
@ -87,8 +89,8 @@ jQuery.fn.extend({
|
|||
for ( i = 0, l = selectors.length; i < l; i++ ) {
|
||||
selector = selectors[i];
|
||||
|
||||
if ( !matches[selector] ) {
|
||||
matches[selector] = jQuery.expr.match.POS.test( selector ) ?
|
||||
if ( !matches[ selector ] ) {
|
||||
matches[ selector ] = POS.test( selector ) ?
|
||||
jQuery( selector, context || this.context ) :
|
||||
selector;
|
||||
}
|
||||
|
@ -96,9 +98,9 @@ jQuery.fn.extend({
|
|||
|
||||
while ( cur && cur.ownerDocument && cur !== context ) {
|
||||
for ( selector in matches ) {
|
||||
match = matches[selector];
|
||||
match = matches[ selector ];
|
||||
|
||||
if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
|
||||
if ( match.jquery ? match.index( cur ) > -1 : jQuery( cur ).is( match ) ) {
|
||||
ret.push({ selector: selector, elem: cur, level: level });
|
||||
}
|
||||
}
|
||||
|
@ -111,8 +113,10 @@ jQuery.fn.extend({
|
|||
return ret;
|
||||
}
|
||||
|
||||
var pos = POS.test( selectors ) ?
|
||||
jQuery( selectors, context || this.context ) : null;
|
||||
// String
|
||||
var pos = POS.test( selectors ) || typeof selectors !== "string" ?
|
||||
jQuery( selectors, context || this.context ) :
|
||||
0;
|
||||
|
||||
for ( i = 0, l = this.length; i < l; i++ ) {
|
||||
cur = this[i];
|
||||
|
|
|
@ -182,6 +182,20 @@ test("closest(Array)", function() {
|
|||
same( jQuery("body").closest(["span","html"]), [{selector:"html", elem:document.documentElement, level:2}], "closest([body, html])" );
|
||||
});
|
||||
|
||||
test("closest(jQuery)", function() {
|
||||
expect(7);
|
||||
var $child = jQuery("#nothiddendivchild"),
|
||||
$parent = jQuery("#nothiddendiv"),
|
||||
$main = jQuery("#main");
|
||||
ok( $child.closest( $parent ).is('#nothiddendiv'), "closest( jQuery('#nothiddendiv') )" );
|
||||
ok( $child.closest( $parent[0] ).is('#nothiddendiv'), "closest( jQuery('#nothiddendiv') ) :: node" );
|
||||
ok( $child.closest( $child ).is('#nothiddendivchild'), "child is included" );
|
||||
ok( $child.closest( $child[0] ).is('#nothiddendivchild'), "child is included :: node" );
|
||||
equals( $child.closest( document.createElement('div') ).length, 0, "created element is not related" );
|
||||
equals( $child.closest( $main ).length, 0, "Main not a parent of child" );
|
||||
equals( $child.closest( $main[0] ).length, 0, "Main not a parent of child :: node" );
|
||||
});
|
||||
|
||||
test("not(Selector)", function() {
|
||||
expect(7);
|
||||
equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );
|
||||
|
|
Loading…
Reference in a new issue