出于某种原因,我不知道为什么我在Backbone View中的事件不起作用.我试图谷歌寻求答案,但我找不到任何可以帮助我的东西.
基本上,我的代码是这样的:
var ViniView = Backbone.View.extend({
el: $('.container'),
events: {
"click .clickme" : "render"
},
render: function() {
alert("please, work");
}
});
new ViniView;
Run Code Online (Sandbox Code Playgroud)
<div class="container">
<button class="clickme">
test
</button>
</div>
Run Code Online (Sandbox Code Playgroud)
你的例子在这个小提琴中对我来说效果很好。
不过,正如 explunit 所指出的,您el应该引用一个元素,而不应该是 jQuery 对象。$el照顾这个。根据文档:
All views have a DOM element at all times (the el property), whether they've already been inserted into the page or not.
Run Code Online (Sandbox Code Playgroud)
检查您是否正确加载了 Jquery、Underscore 和 Backbone 脚本(按顺序)。还要确保您的脚本在页面准备好后执行,而不是在 DOM 完成加载之前执行(导致您的视图不附加到任何内容)。
| 归档时间: |
|
| 查看次数: |
4702 次 |
| 最近记录: |