我試圖刪除事件偵聽器的函數調用後。但是,無論我嘗試什麼,「密碼」的事件監聽器都會附着在身體上。代碼有什麼問題?removeEventListener不工作
function displayImage() {
//this is a simplified version of the code
var outerFrame = document.createElement('div');
outerFrame.className = 'popup-outer';
document.body.appendChild(outerFrame);
document.body.addEventListener('keyup', hideImage.bind(outerFrame), false);
}
function hideImage(e) {
if (e.keyCode === 27) {
// this doesn't work, it stays attached to the body element
document.body.removeEventListener('keyup', hideImage, false);
document.body.removeChild(this);
}
e.preventDefault();
}
可能重複[使用Javascript removeEventListener不工作 - 事件偵聽器仍然](http://stackoverflow.com/questions/16651790/javascript-removeeventlistener-not-working-event-listener-remains) – Amit