Javascript JS/Backbone/Chaplin事件处理程序未触发
在卓别林视图中,在动态生成的元素上绑定事件处理程序时遇到问题,我无法弄清楚发生了什么。这似乎是最明确的可能实现:Javascript JS/Backbone/Chaplin事件处理程序未触发,javascript,backbone.js,Javascript,Backbone.js,在卓别林视图中,在动态生成的元素上绑定事件处理程序时遇到问题,我无法弄清楚发生了什么。这似乎是最明确的可能实现: var MyView = Chaplin.View.extend({ /* using jQuery to bind the event because Chaplin.View.listen and Chaplin.View.delegate aren't working... */ render: function() { Cha
var MyView = Chaplin.View.extend({
/* using jQuery to bind the event because Chaplin.View.listen
and Chaplin.View.delegate aren't working... */
render: function() {
Chaplin.View.prototype.render.apply(this, arguments);
this.$('#the-button').click(function() { console.log('clicked'); });
console.log('breakpoint here');
}
});
在Chrome开发工具中:
> this.$('#the-button').attr('unique-attr', 'blah');
< [ <button id="the-button" unique-attr="blah">Text</button>]
> this.$('#the-button').click()
clicked
< [ <button id="the-button" unique-attr="blah">Text</button>]
取消暂停应用程序,确保我们正在查看相同的元素:
> $('#the-button')
< [ <button id="the-button" unique-attr="blah">Text</button>]
> $('#the-button').click()
[ no output ]
< [ <button id="the-button" unique-attr="blah">Text</button>]
有人能解释一下为什么按钮的onclick事件处理程序在渲染函数的作用域中被触发,而不是在全局作用域中被触发吗?谢谢。解决了
我在父视图中指的是MyView.$el.html。返回元素的innerHTML。但是事件处理程序绑定到$el的根,该根不再呈现到文档中,因此没有调用事件处理程序