简体   繁体   中英

Jquery 'on' event with .bind(this). How to access $(this)?

I register an event

$('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');

How can I access the right button element without $(this)? Or can i access $(this) within my method?

this.method will get the Event object passed as first argument. If you define the function to have a parameter event , you can reference the element via event.target .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM