简体   繁体   English

为什么不能删除EventListener

[英]Why can't removeEventListener

code link 代码链接

I want to remove mouseover event when mouseup event triggers, but why can't revomeEventListener and how to fix it,looks like this is my weak part in javascript. 我想在mouseup事件触发时删除mouseover事件,但是为什么不能revomeEventListener及其解决方法,这似乎是我在javascript中的薄弱环节。

In your fnDown you set the event listener to be on the dragBox but then you try to remove it from the dragArea later on. 在您的fnDown中,将事件侦听器设置为dragBox,但是稍后尝试将其从dragArea中删除。 You need to attach the listener to the dragArea if that's where you plan to remove it from. 如果您打算将侦听器从dragArea中删除,则需要将其附加到该侦听器。 This function works as expected. 此功能按预期方式工作。

    Drag.prototype.fnDown = function (e) {
        this.diff.x = e.clientX - this.dragBox.offsetLeft
        this.diff.y = e.clientY - this.dragBox.offsetTop
        this.dragArea.addEventListener('mousemove', this.fnMove,false)
    }

Working example: http://jsbin.com/dugoxeziho/1/edit?html,output 工作示例: http : //jsbin.com/dugoxeziho/1/edit?html,输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM