0
我试图做到这一点:FOO的jquery事件不适用于对象?
var foo = new Foo();
foo.setEvents(foo);
代码:
function Foo()
{
this.id = getId(); //this works successfully, each instance of foo gets
//assigned a new auto incrementing id e.g 1, 2, 3, etc
this.setEvents = function(that)
{
$("#myButton").click(that.bar);
}
this.bar = function()
{
alert(this.id);
}
}
每当我运行此代码,然后点击我的按钮,我得到一个警告说“未定义”
如何在触发事件时保留其要运行的方法的对象的状态?
你的手柄和头像让我想downvote这个问题。 –
[它适用于我](http://jsfiddle.net/alexdickson/DAPYt/)。 – alex
@alex他期待得到对象的id而不是按钮。 –