7

出于某种原因,我不知道为什么我在主干视图中的事件不起作用。我试图谷歌一些答案,但我没有找到任何对我有帮助的东西。

基本上,我的代码是这样的:

骨干:

var ViniView = Backbone.View.extend({
    el: $('.container'),
    events: {
        "click .clickme" : "render"
    },
    render: function() {
        alert("please, work");
    }
});
    
new ViniView;

HTML

<div class="container">
    <button class="clickme">
    test
    </button>
</div>
4

1 回答 1

4

你的例子在这个小提琴中对我来说很好。

但是,正如 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.

检查您是否正确加载了 Jquery、Underscore 和 Backbone 脚本(按此顺序)。还要确保在页面准备就绪后执行脚本,而不是在 DOM 完成加载之前执行(导致您的视图不附加到任何内容)。

于 2013-03-22T17:05:26.683 回答