jQuery用不同的绑定

Abi*_*bid 3 javascript jquery

我想将一个函数绑定到一个事件,但我想更改调用该函数的上下文

function Foo(){

  t : "123",
  bar: function(){
    // I am here
    $('#selector').bind('click' this.foo);
  }
  ,
  foo: function(){
    //I want when to be able to use the current object here by this
    //this.t should be 123 and this.bar should call the above function
  }
}
Run Code Online (Sandbox Code Playgroud)

Eng*_*eer 5

你可以使用jQuery.proxy:

$('#selector').bind('click', $.proxy(this.foo,this) );
Run Code Online (Sandbox Code Playgroud)