Javascript:`this`和object.method事件处理程序

时间:2014-03-20 23:18:44

标签: javascript jquery methods this

我正在尝试从对象中获取一堆el并将方法作为单击处理程序附加,并将单击的el传递给该函数。代码看起来像这样(jQuery):

svCatEditor.list.bind('click', svCatEditor.controller($(this)));

当我在$(this)中控制{。{}}时,我得到了svCatEditor.controller个对象。我理解为什么会这样:http://unschooled.org/2012/03/understanding-javascript-this/

我不明白的是如何绕过它。我是否需要采取完全不同的方法,或者是否有一些我缺少的简单方法?

提前致谢。

1 个答案:

答案 0 :(得分:2)

你必须按照以下方式进行:

svCatEditor.list.bind('click', function() { svCatEditor.controller($(this)) } );
回调函数的上下文中的

this指的是触发事件处理程序的当前对象。否则在您的外部环境中this指向窗口。

相关问题