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

贴一个自己写的多播,,望指教
JScript code

   /**
 * @author changchuan
 * @用途:用来为Dom节点添加事件多播,回调函数(类似,attachevent)
 * @参数:oDom,添加事件的dom节点。eventType,事件类型。oFun,要添加的事件回调函数
 * @example:function testFun(){
                alert(arguments[0].type);
            }
            function testFun2(){
                alert(this.id+"asdasd");
            }
            Dom.addLisenner(document.getElementById("testInput"),"onclick",testFun);
            Dom.addLisenner(document.getElementById("testInput"),"onclick",testFun2);
            //Dom.remLisenner(document.getElementById("testInput"),"onclick",testFun2);    
 *             
 */
var Dom={};
Dom.addLisenner= function (oDom,eventType,oFun){
    var oEl=oDom,e=eventType,funhandle=oFun,oEvArr=[];    
    if(typeof(oEl.oEvArr)=="undefined"){
            oEl.oEvArr=oEvArr;                
        }
    if(typeof(oEl.oEvArr[e])=='undefined'){
        oEl.oEvArr[e]=[];
    }
    oEl.oEvArr[e].push(funhandle);    
    if(!oEl[e]){
        oEl[e]=function(){
                            var ev=arguments[0]||window.event;            
                            var oEvArr=this.oEvArr['on'+ev.type];
                            var el=this;
                            for(var i=0;i<oEvArr.length;i++){
                                if(oEvArr[i]){
                                    oEvArr[i].call(el,ev);
                                }
                            }
                        }
    }
}
Dom.remLisenner=function (oDom,eventType,oFun){            
    if(!oDom||!eventType||!oFun||typeof(oDom.oEvArr)=='undefined'||typeof(oDom.oEvArr[eventType])=='undefined'){
        return false;
    }
    var oEvArr=oDom.oEvArr[eventType];
    for(var i=0;i<oEvArr.length;i++){
        if(oEvArr[i]==oFun){
            oEvArr[i]=null;
            return true;
        }
    }
    return false;
}        
/**********************Dom.addLisenner  Dom.remLisenner  End****************************/    



------解决方案--------------------
多播是什么意思?

跟这种有什么分别?
function addEventHandler(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
};
------解决方案--------------------
什么是多播?
学习下..