日期:2014-05-16  浏览次数:20368 次

[ExtJS3.2源码每天一小时]ext-base.js的getDom与removeNode(之六)
  el:要获取的dom对象对应的对象,这个对象可以是ID,可以是Ext的Element对象。
  getDom : function(el, strict){
            if(!el || !DOC){
                return null;
            }
            //如果是Ext的Element对象,直接返回其dom属性
            if (el.dom){
                return el.dom;
            } else {
                //如果是ID值
                if (typeof el == 'string') {
                    //调用原生api获得dom节点对象e
                    var e = DOC.getElementById(el);
                    // IE returns elements with the 'name' and 'id' attribute.
                    // we do a strict check to return the element with only the id attribute
                    //如果e不为空、浏览器是IE、且校验得到对象的id与原始el
                    if (e && isIE && strict) {
                        if (el == e.getAttribute('id')) {
                            return e;
                        } else {
                            return null;
                        }
                    }
                    return e;
                } else {
                    //除两种情况之外的,直接return el
                    return el;
                }
            }
        }



    //获得body对象 兼容各种浏览器
    getBody : function(){
            return Ext.get(DOC.body || DOC.documentElement);
    }



//删除节点 分别真对ie8之前的版本 和 ie8及其它浏览器采用不同的方式处理
removeNode : isIE && !isIE8 ? function(){
            var d;
            return function(n){
                if(n && n.tagName != 'BODY'){
                    (Ext.enableNestedListenerRemoval) ? Ext.EventManager.purgeElement(n, true) : Ext.EventManager.removeAll(n);
                    //想将要删除的节点加到一个虚拟节点上,最后将虚拟节点的innerHtml属性置空,达到删除目的
                    d = d || DOC.createElement('div');
                    d.appendChild(n);
                    d.innerHTML = '';
                    //删除对应的缓存
                    delete Ext.elCache[n.id];
                }
            }
        }() : function(n){
            if(n && n.parentNode && n.tagName != 'BODY'){
                (Ext.enableNestedListenerRemoval) ? Ext.EventManager.purgeElement(n, true) : Ext.EventManager.removeAll(n);
                n.parentNode.removeChild(n);
                delete Ext.elCache[n.id];
            }
        }