Javascript removeEventListener not working - event listener remains

后端 未结 3 503
一生所求
一生所求 2021-01-20 06:05

I\'ve looked at a few ways around this, but I can\'t really tell, my code is:

lb = document.body;

if(lb.addEventListener){    
    lb.addEventListener(\'key         


        
3条回答
  •  别那么骄傲
    2021-01-20 06:46

    you cant remove an anonymous function with removeEventListener, instead use the function name. e.g:

    if(lb.addEventListener){    
        lb.addEventListener('keyup', myFunction, false);
    }
    
    //In another function.
    
    if(document.body.removeEventListener){
        document.body.removeEventListener('keyup', myFunction, false);
    } 
    

    the new function:

    function myFunction(e){
        var keyCode = e.keyCode;
    
    }
    

提交回复
热议问题