cly*_*yfe 8 javascript jquery events binding this
在jquery中,事件hadler的绑定是生成DOM元素的事件(这指向dom元素).在原型中更改事件处理程序的绑定,可以使用bindAsEventListener函数; 如何从事件处理程序访问实例和DOM元素?
类似于如何将事件处理程序绑定到JQuery中的实例?
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();
Run Code Online (Sandbox Code Playgroud)