Ya que soy incapaz de responder como un comentario (demasiado bajo Karma, supongo), he aquí una respuesta completa. La manera más rápida es desenrollar fácilmente la verificación de jQuery en busca de compatibilidad con el navegador y reducir al mínimo los factores constantes.
Como se ve también aquí - http://jsperf.com/jquery-element-in-dom/28 - el código se vería así:
var isElementInDOM = (function($) {
var docElt = document.documentElement, find,
contains = docElt.contains ?
function(elt) { return docElt.contains(elt); } :
docElt.compareDocumentPosition ?
function(elt) {
return docElt.compareDocumentPosition(elt) & 16;
} :
((find = function(elt) {
return elt && (elt == docElt || find(elt.parentNode));
}), function(elt) { return find(elt); });
return function(elt) {
return !!(elt && ((elt = elt.parent) == docElt || contains(elt)));
};
})(jQuery);
Esta es semánticamente equivalente a jQuery.contains (document.documentElement, elt [0]).
+1 eso es slick :) –
haciendo '$ foo.closest (document.documentElement)' es más rápido (si a alguien le importa http://jsperf.com/jquery-element-in-dom) – urraka
@PerroAzul: Encontré una alternativa _much_ más rápida: http://jsperf.com/jquery-element-in-dom/2 – SLaks