Javascript removeEventListener not working - event listener remains

后端 未结 3 505
一生所求
一生所求 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 07:00

    You need to name the handler function in order to reference it later when removing the listener:

    lb.addEventListener('keyup', 
    function keyup(event){ // assigns the handler to "keyup"
        keyPress(event.keyCode)
    }, false);
    
    document.body.removeEventListener('keyup', keyup, false);
    

提交回复
热议问题