码迷,mamicode.com
首页 > 其他好文 > 详细

添加事件与移除事件

时间:2018-11-08 23:20:51      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:匿名   回调函数   lis   listener   hand   listen   add   文档   and   

第一种:

通过addEventListener添加事件,必须通过removeEventListener移除事件,并且回调与参数都必须一致,所以如下情况使用匿名回调函数是无法移除事件的。

不起效:

document.addEventListener(‘mousedown‘, function() {

  console.log(‘添加文档的鼠标按下事件‘);

});

document.removeEventListener(‘mousedown‘, function() {

  console.log(‘移除文档的鼠标按下事件,但是这样是不起效的。‘);

});

起效:

let handler = function(e) {

  console.log(‘回调函数,并且事件对象Event默认会传进来‘);

}

document.addEventListener(‘mousedown‘, handler);

document.removeEventListener(‘mousedown‘, handler);

 

添加事件与移除事件

标签:匿名   回调函数   lis   listener   hand   listen   add   文档   and   

原文地址:https://www.cnblogs.com/coconutGirl/p/9932351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!