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

javascript add event remove event_javascript技巧

WBOY
Release: 2016-05-16 19:05:23
Original
1024 people have browsed it

网上搜来的,看样子不错,记一笔。//------------------------------------
// heavily based on the Quirksmode addEvent contest winner, John Resig
// addEvent
function addEvent(obj,type,fn){
    if(obj.addEventListener) obj.addEventListener(type,fn,false);
    else if(obj.attachEvent){
        obj["e" type fn]=fn;
        obj[type fn]=function(){obj["e" type fn](window.event);}
        obj.attachEvent("on" type,obj[type fn]);
    }
}

//------------------------------------
// removeEvent
function removeEvent(obj,type,fn){
  if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
  else if(obj.detachEvent){
    obj.detachEvent("on" type,obj[type fn]);
    obj[type fn]=null;
    obj["e" type fn]=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