function contains(a, b){
    //元素a是否包含元素b
    if (document.compareDocumentPosition) {
        return !!(a.compareDocumentPosition(b) & 16);
    }
    else {
        return a !== b && (a.contains ? a.contains(b) : true);
    }
}

参考:http://blog.csdn.net/huajian2008/article/details/3960343