骨干 - 其中"这"是"这个"?

时间:2014-07-18 10:10:52

标签: javascript jquery backbone.js

虽然我的知识仍然非常有限,但我不得不在工作中使用Backbone。

我很难理解“这个”元素 - 这是一个例子:

通常,这将是一段jQuery代码:

$('.birds').click(function() {
    $(this).fadeOut();
});

我想要实现的是点击同一个类中的一个元素(例如.birds),以便能够对这个特定的单击元素执行操作。

我看到虽然在视图中经常使用“this”,但我甚至不知道从哪里开始将“this”改为“this”。

我很欣赏关于Backbone中这个内容的简短解释。

编辑:

感谢您的链接,我会读到它。在此期间 - 假设这种结构:

events: {
    'click .birds' : 'birdsFunction'
},

birdsFunction: {
    $(this).fadeOut();
}

是否正确写入或我认为“this”(带有.birds类的点击元素)不是我的“这个”?

1 个答案:

答案 0 :(得分:1)

在Backbone类的方法中,this是对类对象的引用。 因此,在事件侦听器方法中,如果需要访问触发事件的DOM节点,请使用event参数。

events: {
    'click .birds' : 'birdsFunction'
},

birdsFunction: function(event) {
    // Here (inside the method of class) this will point to current view
    // and not to the DOM node.
    // So let's get this node from event.currentTarget
    // or from event.target property.
    $(event.currentTarget).fadeOut();
}