JavaScript 的 addEventListener 与 removeEventListener

最近使用JS监听命令时,发现了很多知识点不足,在此整理一下。

正确用法

1 //禁止动作
2  function noScroll(e){
3    e.preventDefault(); 
4 }
5  //添加事件 
6 document.addEventListener('touchmove',noScroll,false);
7  //接触绑定 
8 document.removeEventListener('touchmove',noScroll,false);

 

错误用法⑴

1 function noScroll(e){
2   e.preventDefault(); 
3 } 
4 document.addEventListener('touchmove',noScroll(e),false); 
5 document.removeEventListener('touchmove',noScroll(e),false); 

共用一个noScroll方法,不能带参数。

 

错误用法⑵

1 document.addEventListener('touchmove', function(e){e.preventDefault();},false); 
2 document.removeEventListener('touchmove', function(e){e.preventDefault();},false);

添加事件和解除绑定要使用同一个Function,否则不能成功解绑。

posted @ 2016-05-26 10:04  小仙桃  阅读(473)  评论(0编辑  收藏  举报