2013-05-08 21 views
1

我是Backbone的新手,理解这个想法,但是在编写简单的toggleClass功能时遇到了麻烦。我的网站是一个正方形的网格,当一个广场有“悬停”的类时,一些CSS导致外观改变(显然)。我的问题是toggleClass无法正常工作。我的代码如下:骨干 - 悬停的toggleClass

var IndexView = Backbone.View.extend({ 
el: $('#main'), 
indexTemplate: $("#indexSquare").template(), 

events: { 
"mouseover .square" : "mouseovercard" 
}, 

render: function() { 
    removeFallbacks(); 
    var sg = this; 

    this.el.fadeOut('fast', function() { 
    sg.el.empty(); 
    $.tmpl(sg.indexTemplate, sg.model.toArray()).appendTo(sg.el); 
    sg.el.fadeIn('fast'); 
    }); 
    return this; 
}, 

mouseovercard: function() { 

    $(this).toggleClass('hover') 
    console.log("hey you're hovering!") 

} 

}); 

这到底是什么错误?任何帮助都感激不尽!

回答

3

this在您的代码中指的是View对象,使用currentTarget属性的event对象。

mouseovercard: function(event) { 
    $(event.currentTarget).toggleClass('hover'); 
    console.log("hey you're hovering!"); 
} 
1

可以使用的骨干查看$el属性:

this.$el.toggleClass('hover');