问答详情
源自:8-1 总结

addEventListener兼容问题??

监听事件addEventListener不兼容  怎么处理啊……

提问者:齐7 2015-11-04 15:38

个回答

  • 李晓健
    2015-11-07 13:55:00

    var addEvent=(function(){
        if(document.addEventListener){
            return function(el,type,fn){
                if(el.length){
                    for(var i=0;i<el.length;i++){
                        addEvent(el[i],type,fn);
                    }
                }else{
                    el.addEventListener(type,fn,false);
                }
            };
        }else{
            return function(el,type,fn){
                if(el.length){
                    for(var i=0;i<el.length;i++){
                        addEvent(el[i],type,fn);
                    }
                }else{
                    el.attachEvent('on'+type,function(){
                        return fn.call(el,window.event);
                    });
                }
            };
        }
    })();

    判断处理下就行了