在 jquery 中,事件处理程序的绑定是事件生成 DOM 元素(this 指向 dom 元素)。
在原型中,要更改事件处理程序的绑定,可以使用绑定为事件监听器 http://www.prototypejs.org/api/function/bindaseventlistener功能;
如何从事件处理程序访问实例和 DOM 元素?
如同如何将事件处理程序绑定到 JQuery 中的实例? https://stackoverflow.com/questions/117361/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();
嗯,也许你可以使用 jQuery.proxy() ?
http://api.jquery.com/jQuery.proxy/ http://api.jquery.com/jQuery.proxy/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)