M 尝试在我的 Backbone View 中提供 mouseover
事件,这是我的 View :
Backbone.View.extend({
template :_.template( '<li class="<% if (refertype=="U"){%>info <% }else{%> access<%}%> main"><%=refername%>'+
'</li>'),
initialize: function() {
_.bindAll(this, 'render', 'close');
this.model.bind('change', this.render);
this.model.view = this;
},
events: {
"mouseover .main": "mouseovercard"
},
// Re-render the contents of the Card item.
render: function() {
this.el=this.template(this.model.toJSON());
$(".cards-list").append(this.el);
},
mouseovercard: function() {
console.log("hello world");
}
});
但是当我将鼠标悬停在 main
类上时,它没有显示 hello world
,请建议该怎么办?
尝试过 Heikki Answer 但鼠标悬停不起作用?
App.Backbone.CardView = Backbone.View.extend({
tagName: 'li',
className: 'main',
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.model.view = this;
},
events:{
"mouseover .main": "mouseovercard"
},
// Re-render the contents of the Card item.
render: function() {
$(this.el)
.removeClass('info access')
.addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
.text(this.model.get('refername'));
$(".cards-list").append(this.el);
},
mouseovercard: function() {
console.log("hello world");
}
});
最佳答案
您正在替换事件绑定(bind)到的 View 的根元素。
试试这个:
Backbone.View.extend({
tagName: 'li',
className: 'main',
events: {
'mouseover': 'mouseovercard'
},
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
},
render: function() {
$(this.el)
.removeClass('info access')
.addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
.text(this.model.get('refername'));
return this;
},
mouseovercard: function() {
console.log('hello world');
}
});
关于jquery - 如何在 Backbone.js 中添加鼠标悬停事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6583029/