2013-05-08 37 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');