RemoveEventListener在Firefox ver 58中不起作用

时间:2018-01-26 22:13:42

标签: javascript firefox

但它适用于Chrome。

这是来自UI_EventBus(原型)的代码: (addEventListener方法相同,但remove替换为add。)

UI_EventBus.removeEventListener = function(obj, argEventType, argEventHandler) {
    var element;;
    var strObj = obj.toString();

    if(strObj.indexOf("HTML") > -1) 
        element = obj;
    else
        element = obj.getElement();


    element.removeEventListener(argEventType, function(event){
        argEventHandler(event,obj);
    });

}

我从UI_Event(原型)中调用它:

closeDragElement:   function(event, obj) {
event = event || window.event;
UI_EventBus.removeEventListener(obj,"mouseup", obj.closeDragElement);
UI_EventBus.removeEventListener(document,"mouseup", obj.closeDragElement);
UI_EventBus.removeEventListener(obj,"mousemove", obj.elementDrag);
}

我在UI_Event(原型)中从此方法添加了它:

dragMouseDown:   function(event, obj) {
event = event || window.event;

UI_EventBus.removeEventListener(obj,"mousedown", obj.dragMouseDown,[]);
obj.mouseX = event.clientX;
obj.mouseY = event.clientY;
UI_EventBus.addEventListener(obj,"mouseup", obj.closeDragElement);
UI_EventBus.addEventListener(document,"mouseup", obj.closeDragElement);
UI_EventBus.addEventListener(obj,"mousemove", obj.elementDrag);
}

我从UI_EventTest.html调用了dragMouseDown -handler:

<script>
....
UI_EventBus.addEventListener(elem,"mousedown", elem.dragMouseDown);
..... 
</script>

我花了很多时间试图解决这个问题,但无济于事。如果有人能告诉医生什么是错的,我真的很感激。该解决方案适用于Chrome。

2 个答案:

答案 0 :(得分:2)

removeEventListener的第二个参数必须是您要删除的函数。

你在那里放了一个函数表达式,它将创建一个全新的函数。因为它是全新的,所以以前不能将其添加为事件监听器。

答案 1 :(得分:0)

如果有人对解决方案感兴趣,这是我的更新代码:

UI_EventBus.listeners = [];

/ *   *添加活动聆听者   * /

UI_EventBus.addEventListener = function(target,  argEventType, obj, argEventHandler) {
    var element;;
    var strObj = target.toString();

    if(strObj.indexOf("HTML") > -1) 
        element = target;
    else
        element = target.getElement();




    var func = function(event){ 
        argEventHandler(event,obj); 
    };
    //console.log(" Add : " + target + "  " +  argEventType + " " + obj + " " +  argEventHandler + " " + func + " no of listeners " + UI_EventBus.listeners.length);

    element.addEventListener(argEventType,func);

    var listener = [target,argEventType, obj, argEventHandler, func];



    UI_EventBus.listeners.push(listener);


}
/*
 *   REMOVE EVENT LISTENER
 */

UI_EventBus.removeEventListener = function(target, argEventType, obj, argEventHandler) {
    var element;;
    var strObj = target.toString();

    if(strObj.indexOf("HTML") > -1) 
        element = target;
    else
        element = target.getElement();



    var idx;
    var func;
    for(var i=0; i < UI_EventBus.listeners.length;i++)
        if(UI_EventBus.listeners[i][0] == target)
            //console.log("Obj " + UI_EventBus.listeners[i][0]);
            if(UI_EventBus.listeners[i][1] == argEventType)
                //console.log("Eventtype " + UI_EventBus.listeners[i][1]);
                if(UI_EventBus.listeners[i][2] == obj)
                    if(UI_EventBus.listeners[i][3] == argEventHandler){
                        //console.log("Handler " + UI_EventBus.listeners[i][2]);    
                        func =  UI_EventBus.listeners[i][4];
                        idx = i;
                        break;
                    }


    element.removeEventListener(argEventType,func);


    //console.log("no of listeners " + UI_EventBus.listeners.length + " idx=" + idx);
    //console.log(" Remove : " + UI_EventBus.listeners[i][0] + "  " +  UI_EventBus.listeners[i][1] + " " + UI_EventBus.listeners[i][2] + " " +  UI_EventBus.listeners[i][3] + " " +  UI_EventBus.listeners[i][4] );
    UI_EventBus.listeners.splice(idx,1);


}