gpt4 book ai didi

backbone.js - 主干事件不起作用

转载 作者:行者123 更新时间:2023-12-02 12:17:46 25 4
gpt4 key购买 nike

出于某种原因,我不知道为什么我的主干 View 中的事件不起作用。我尝试在 Google 上寻找答案,但没有找到任何对我有帮助的信息。

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

主干:

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>

最佳答案

你的例子对我来说效果很好 this fiddle .

正如 explunit 所指出的,您的 el 应该引用一个元素,而不应该是一个 jQuery 对象。 $el 负责处理这个问题。根据the docs :

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 完成加载之前执行(导致您的 View 不附加到任何内容)。

关于backbone.js - 主干事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15575820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com