这绝对让我发疯!有人可以解释为什么这段代码不起作用以及如何修复它吗?它应该向控制台发送“单击”消息,但它不起作用。控制台消息在其他任何地方都有效,但由于某种原因,javascript 没有捕获点击事件。
var resultsListView = new ResultsListView({model: comparablesList});
ResultsListView = Backbone.View.extend({
tagName:'div',
id:'resultView',
template:_.template("<div class='resultsListItemView'></div>"),
initialize:function () {
$( this.el ).html('<p class="loadingText">Getting comparables...<p>');
this.model.bind("change", this.render, this);
this.model.bind("reset", this.loading, this);
},
render:function (eventName) {
$( this.el ).html( this.template() );
_.each(this.model.models, function (comparable) {
this.$('.resultsListItemView').append(new ResultsListItemView({model:comparable}).el);
}, this);
$('#info').html($(this.el));
return this;
}
});
ResultsListItemView = Backbone.View.extend({
initialize: function(){
this.render();
},
template:_.template("<div class='hovr_item'> \
<div class='hovr'> \
<button class='test123'>test</button> \
</div> \
</div> "),
render: function(){
var resultsListItemView = this;
$(this.el).html(this.template(this.model.toJSON()));
$(this.el).find('.test123').click(function() {
console.log('click'); // this does not work after clicking
});
return this;
}
});
作为一个额外的线索,如果我将此代码放入渲染中,它确实会显示在控制台中......所以它似乎只是点击事件:
$(this.el).find('.test123').each(function(index) {
console.log('found');
});