GitHub 博客园 Nanakon

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
    }
}

 

 

 

posted on 2017-01-03 17:04  jzm17173  阅读(110)  评论(0编辑  收藏  举报

导航

轻音