Jquery' on'使用.bind(this)的事件。如何访问$(this)?

时间:2015-01-27 17:42:49

标签: javascript

我注册了一个活动

$('button').on('click', this.method.bind(this));

master.prototype.method = function() {
  // This works only if i bind(this) in the event.
  this.otherMethod();

  // The jquery $(this) does not work more...
  $(this).addClass('...');
}


// But that does not work: (I will reference to the 'button')
$(this).addClass('class');

如何在没有$(this)的情况下访问正确的button元素?或者我可以在我的方法中访问$(this)吗?

1 个答案:

答案 0 :(得分:2)

this.method会将Event对象作为第一个参数传递。如果您将函数定义为具有参数event,则可以通过event.target引用该元素。