Home > Web Front-end > JS Tutorial > body text

Common methods about js registration events_javascript skills

WBOY
Release: 2016-05-16 17:38:33
Original
1227 people have browsed it

No explanation, look at the code:

Copy code The code is as follows:

;(function(){
//Library encapsulation to prevent object pollution
window['cm']={};
/**
*Register events for objects
*/
var addListener=function(element,type,fn){
if(typeof element == 'undefined') return false;
if(element.addEventListener){
element.addEventListener(type,fn, false);
}else if(element.attachEvent){
var _EventRef='_' type 'EventRef';
                                                                                                              _EventRefs=element[_EventRef];
var index;
for(index in _EventRefs){
if(_EventRefs[index]['realFn']==fn){
return;
                                                                                                                                                                                                                                                var nestFn=function(){
                  fn.apply(element,arguments); 🎜>           element.attachEvent('on' type, nestFn); ]['addListener']=addListener;
/**
*Remove registered events on the object
*/
var removeListener=function(element,type,fn){
if(typeof element == 'undefined') return false ;
if(element.removeEventListener){
element.removeEventListener(type,fn,false);
}else if(element.detachEvent){
var _EventRef='_' 'EventRef' ;
                                                                                                                                                                                                            🎜> var index;
var nestFn;
for(index in _EventRefs){
if(_EventRefs[index]['realFn']==fn){
nestFn=_EventRefs[index]['nestFn'];
if(index ==_EventRefs.length-1){
                                                                                                                                                           else{
                        element[_EventRef]=_EventRefs.slice(0,index).concat(_EventRefs.slice(index 1,_EventRefs.length-1));
                    }
                    break;
                }
            }
            if(nestFn){
                element.detachEvent('on' type,nestFn);
            }
        }else{
            element['on' type]=null;
        }
    };
})();

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!