【发布时间】:2010-05-19 09:49:04
【问题描述】:
在 jquery 中,事件处理程序的绑定是事件生成 DOM 元素(这指向 dom 元素)。
在原型中更改事件处理程序的绑定可以使用bindAsEventListener 函数;
如何从事件处理程序访问实例和 DOM 元素?
类似于How can I bind an event handler to an instance in 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();
【问题讨论】:
标签: javascript jquery events binding this