jquery에서 이벤트 보유자의 바인딩은 DOM 요소를 생성하는 이벤트입니다 (dom 요소를 가리킴). 프로토 타입에서 이벤트 핸들러의 바인딩을 변경하려면 bindAsEventListener 함수를 사용할 수 있습니다. 이벤트 핸들러에서 인스턴스와 DOM 요소 모두에 액세스하려면 어떻게해야합니까? 유사
그렇지 가리킬 것이다 this
값 How can I bind an event handler to an instance in JQuery?jquery 이벤트 처리기의 "this"바인딩 문제 (프로토 타입의 bindAsEventListener와 동일)
function Car(){
this.km = 0;
$("#sprint").click(this.drive); //setup event handler
}
// event handler
// in it I need to access both the clicked element
// and the binding object (instance of car)
Car.prototype.drive = function(){
this.km += 10; // i'd like to access the binding (but jq changes it)
this.css({ // also the element
left: this.km
});
// NOTE that is inside this function I want to access them not elsewhere
}
var car = new Car();
나는'bindAsEventListener'을 사용할 수 없습니다. – clyfe
@clyfe : 직접 작성하는 것이 매우 간단합니다. –