我创建了一个视图,并提供了以下代码:
var app = app || {};
app.singleFlowerView = Backbone.View.extend({
tagName: 'article',
className: 'flowerListItem',
// tells where to apply the views
template: _.template( $("#flowerElement").html() ),
// render
render: function(){
var flowerTemplate = this.template(this.model.toJSON());
// el contains all the prop above and pass to backbone
this.$el.html(flowerTemplate);
return this;
},
events: {
'mouseover': 'addBgColor',
'mouseout': 'removeBgColor'
},
addBgColor: function(){
this.$el.addBgColor('bgColorImage');
},
removeBgColor: function(){
this.$el.removeBgColor('bgColorImage');
}
});
当我将其运行到HTML文件时,出现错误addBgColor,而removeBgColor不是函数。我有用于此的CSS,并且已设置所有模型和视图。
我在这里想念什么吗?知道为什么事件不起作用吗?
最佳答案
this.$el.addBgColor
是问题。
事件正在触发,但是您正在addBgColor
jQuery对象上调用$el
,这不是jQuery函数,就像错误消息告诉您一样。
检查what's the difference between $el
and el
。