Home  >  Article  >  Web Front-end  >  Common methods for adding and removing functions in javascript_javascript tips

Common methods for adding and removing functions in javascript_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:44:011243browse

//Method to add a function
function addEvent( node, type, listener) {
if( node.addEventListener ){
// W3C method (DOM method)
// false in the following statement It means that it is used in the bubbling phase. If it is true, it is used in the capturing phase (IE does not support capturing), so the reason for using false here is for the sake of unification.
node.addEventListener(type, listener, false);
         return true; = function (){
              node['e' type listener](window.event); true;
}
//If neither method is available, return false
return false;
}


//Method to remove function
function removeEvent( node, type, listener) {
if( node.addEventListener ){
node.removeEventListener( type, listener, false );
return true;
} else if( node.detachEvent ) {
               node.detachEvent('on' type, listener);                                           ; >}


//The method of adding a loading event is as follows
1.function addLoadEvent(func){
var oldonload=window.onload;
if(typeof window .onload!="function"){
                                                                                                                                                                                                                                . 🎜>     }
  }
}
2.//Use the addEvent method above
addEvent(window, 'load', fn);

Statement:
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