Created
May 26, 2010 20:11
-
-
Save remi/414981 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instead of using $('div').parent().parent().parent().parent().addClass('lol') | |
// you can use this: $('div').parentx(4).addClass('lol') | |
if ( window.jQuery ) { | |
jQuery.fn.parentx = function( i ) { | |
if ( i > 1 ) { | |
var e = $(this); | |
for ( var a = 1; a <= i; a++ ) { | |
e = e.parent(); | |
} | |
return e; | |
} | |
return $(this).parent(); | |
} | |
} |
Effectivement, peut-être que le for
est un peu gourmand en termes de ressources. Cependant, un index inexistant dans ton []
pourrait causer une erreur. Je teste ça et je reviens avec les résultats ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
D'accord avec le fonction, mais ne devrait-elle pas faire:
if ( i > 1 ) {
var p = $(this).parents()[i -1]
p = $(p);
return p;
}
Je sais pas si c'est plus performant (c'est pas testé non plus comme code, simplement une réflexion).