Javascript Jquery';在';事件与.bind(此)。如何访问$(这个)?
我登记一个事件Javascript Jquery';在';事件与.bind(此)。如何访问$(这个)?,javascript,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('...'); } //
$('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),如何访问右侧的
按钮元素?或者我可以在方法中访问$(this)?此方法将获取作为第一个参数传递的事件对象。如果将函数定义为具有参数事件
,则可以通过引用元素。请添加完整的示例,而不是在同一代码块中添加三个不相关的片段。不可能知道你在做什么。$('button').on('click',function(){this.method.bind(this)})代码>??@Guilhermanascimento:那不行。@FelixKling但问题不清楚(在作者编辑之前),我知道他想在点击后访问。因此,在理解问题的范围内(在作者编辑之前)它是有意义的。@FelixKling我想我理解,这个方法
变成函数
没有意义,那是在匆忙的时候。:)很抱歉