Skip to content

Instantly share code, notes, and snippets.

@fredyang
Created February 19, 2012 21:42
Show Gist options
  • Save fredyang/1865966 to your computer and use it in GitHub Desktop.
Save fredyang/1865966 to your computer and use it in GitHub Desktop.
isAncestor and isDescendant
$.fn.isAncestor = function( elem ) {
var $elem = $( elem );
if (this.is( $elem )) {
return false;
}
while (($elem = $elem.parent()).length) {
if (this.is( $elem )) {
return true;
}
}
return false;
};
$.fn.isDescendant = function( elem ) {
return $(elem).isAncestor(this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment