Created
August 13, 2008 03:51
-
-
Save kangax/5191 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
Element.Methods.descendantOf = (function() { | |
var doc = document.documentElement; | |
// DOM Level 3 approach (Gecko, Opera) | |
if ('compareDocumentPosition' in doc) { | |
return function(element, ancestor) { | |
return (element.compareDocumentPosition(ancestor) & 8) === 8 | |
}; | |
} | |
// IE, Safari | |
if ('contains' in doc) { | |
return function(element, ancestor) { | |
return ancestor !== element && ancestor.contains(element) | |
}; | |
} | |
// fallback on while loop | |
return function(element, ancestor) { | |
while (element = element.parentNode) | |
if (element === ancestor) return true; | |
return false; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment