Jquery backbone.js单击事件未触发
正在学习有关backbone.js视图的基础教程 预期的行为是在单击#sayhello按钮时调用render函数。Render只是使用jQuery的html方法将“hello Bud Abbot”放在el中 但当我点击#sayhello按钮时,什么也没发生。没有错误或任何东西。我在firebug中设置了一个断点,然后看着它跳过渲染函数 以下是js:Jquery backbone.js单击事件未触发,jquery,backbone.js,Jquery,Backbone.js,正在学习有关backbone.js视图的基础教程 预期的行为是在单击#sayhello按钮时调用render函数。Render只是使用jQuery的html方法将“hello Bud Abbot”放在el中 但当我点击#sayhello按钮时,什么也没发生。没有错误或任何东西。我在firebug中设置了一个断点,然后看着它跳过渲染函数 以下是js: App = (function($){ jQueryView = Backbone.View.extend({ initialize: fu
App = (function($){
jQueryView = Backbone.View.extend({
initialize: function(){
this.el = $(this.el);
}
});
HelloWorldView = jQueryView.extend({
el: $('#helloworld'),
events:{ 'click #sayhello': 'render'
},
initialize: function(params){
jQueryView.prototype.initialize.call(this);
this.name = params.name;
},
render: function(){
console.log("rendering");
this.el.html("hello " + this.name);
}
});
var self = {};
self.start = function(){
new HelloWorldView({name: 'Bud Abbot'});
};
return self;
});
下面是html:
<div id="helloworld"></div>
<button id="sayhello">Say Hello</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
为此:
new HelloWorldView({name: 'Bud Abbot'}).render();
调用了render方法,但是当我尝试使用事件执行时-没有骰子。非常感谢您能帮助我理解我做错了什么。这是因为#说你好不属于您的观点。试着把“说你好”放在“helloworld”里面:
<div id="helloworld">
<button id="sayhello">Say Hello</button>
</div>
打招呼
< /代码> 如果您使用模板动态呈现页面,则在呈现函数中考虑调用视图.StEngEnter(…);
this.setElement($(“#登录容器”)
<div id="helloworld">
<button id="sayhello">Say Hello</button>
</div>