Node.prototype.contains

document.documentElement.contains(document.body) // true
document.documentElement.compareDocumentPosition(document.body) // 20
//safari5+是把contains方法放在Element.prototype上而不是Node.prototype
if (!DOC.contains) {
    Node.prototype.contains = function (arg) {
        return !!(this.compareDocumentPosition(arg) & 16)
    }
}
avalon.contains = function(root, el) {
    try {
        while ((el = el.parentNode))
            if (el === root)
                return true
        return false
    } catch (e) {
        return false
    }
}

原文地址:https://www.cnblogs.com/jzm17173/p/6245557.html