에 mdn "나는 제거한 후에도 EventListener를 호출 할 수 없습니다."라고 읽었지 만, 다시는 추가 할 수 없다는 의미는 아닙니다. 이것은 내가하는 일의 단순화 된 예입니다. 따라서이 예에서 사소한 구문 오류가있는 경우 무시할 수 있습니다 (구문 오류가 문제가 아니면).removeEventListner를 사용한 후에 왜 addEventListener를 사용할 수 없습니까?
function OhYeah(el){
this.stuff = [];
this.stuff.push(new Obj(el));
}
OhYeah.prototype = {
removeStuff: function() {
for(var i = 0; i < this.stuff.length; i++){
this.stuff[i].selfDestruct(); // removes listener
}
this.stuff = [];
},
addStuff: function(el) {
this.stuff.push(new Obj(el)); // should add listener on creation of Obj
}
}
function Obj(el) {
// some other properties not shown that can be different even if the same "el" is used to create a new Obj
this.domOBJ = document.getElementById(el);
this.domOBJ.addEventListener("input", this, false);
}
Obj.prototype = {
...
handleEvent: function(){
...
},
selfDestruct: function() {
this.domOBJ.removeEventListener("input", this, false);
}
}
var obj = new OhYeah("demo"); // adds listener successfully
obj.removeStuff(); // removes listener successfully
obj.addStuff("demo") // you would think this would add the listener, BUT it does NOT
'.addEventListener ("input", this' <- this ????? HUH ???? – epascarello
@ epascarello * Obj * 프로토 타입에 * handleEvent * 메서드가 있기 때문에 작동합니다. 핸들러는 이벤트를 처리 할 때 호출합니다. [* W3C DOM 2 이벤트 EventListener 인터페이스 *] (http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events - * EventListener) - [* DOM 3 Events spec *] (http://www.w3.org/TR/DOM-Level-3-Events/#interface-EventListener)에서 업데이트 됨. – RobG
[* MDN *] (https : //developer.mozilla.org/en-US/docs/Web/API/EventTarget.removeEventListener)는 "* 제거 된 후에 절대로 호출 할 수 없습니다 *"더 이상 말하지 않습니다. ;-) – RobG