Click事件中的“查看Backbone.js的”不工作 [英] Click Event in 'Backbone.js View' do not work
问题描述
我试着开始与Backbone.js的工作,但我发现,当我不使用身体作为视图的El事件不起作用。
这里是code。你可以将其保存为HTML文件并运行它。
< HTML和GT;
<身体GT;
<按钮的ID ='openEssay'>测试与LT; /按钮>
< DIV ID ='格'风格=宽度:100像素,高度:100像素;'>< / DIV><脚本SRC =https://ajax.googleapis.com/ajax/libs/jquery/1.6 .1 / jquery.min.js>< / SCRIPT>
&所述; SCRIPT SRC =http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js>&下; /脚本>
&LT;脚本src=\"http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js\"></script><script>
(函数($){
VAR APPVIEW = Backbone.View.extend({ EL:'身体',//成功
//失败EL:#DIV',
//失败的标签名:礼,
//失败ID:'格',
初始化:功能(){
_.bindAll(这一点,openEssay');
},
事件:{
点击按钮#openEssay':'openEssay
},
openEssay:功能(){
警报('一个');
}
});
VAR应用=新APPVIEW();
})(jQuery的); &LT; / SCRIPT&GT;
&LT; /身体GT;
&LT; / HTML&GT;
骨干结合事件处理程序的视图的 this.el
使用的 在
(或的 委托
中老年骨干网),请参见 Backbone.View#delegateEvents
了解详情。所以,如果你想要这些事件:
事件:{
点击按钮#openEssay':'openEssay
}
做任何事情,然后这一点。$ el.find('#键openEssay')
需要匹配的东西(其中这个
,当然,视图对象)。只有你的四次尝试之一:
-
报:身体
-
报:#DIV'
-
的tagName:礼
-
ID:'DIV'
会把&LT;按钮ID =openEssay&GT;
在 this.el
所以只有 (1)将调用 openEssay
当你点击按钮。如果你把你的按钮在 #div
和(2)也将工作。
I tried to start to work with backbone.js, but I found the Event does not work when I do NOT use 'body' as the View's el. Here is the code. You can save it as a html file and run it.
<html>
<body>
<button id='openEssay'>test</button>
<div id='div' style='width:100px;height:100px;'></div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script><script>
(function($){
var AppView = Backbone.View.extend({
el:'body',//success
//fail el:'#div',
//fail tagName: 'li',
//fail id:'div',
initialize:function(){
_.bindAll(this, 'openEssay');
},
events:{
'click button#openEssay':'openEssay'
},
openEssay:function(){
alert('a');
}
});
var app = new AppView();
})(jQuery);
</script>
</body>
</html>
Backbone binds the event handlers to the view's this.el
using the delegation form of on
(or delegate
in older Backbones), see Backbone.View#delegateEvents
for details. So if you want these events:
events: {
'click button#openEssay':'openEssay'
}
to do anything then this.$el.find('button#openEssay')
needs to match something (where this
is, of course, the view object). Only one of your four attempts:
el: 'body'
el:'#div'
tagName: 'li'
id: 'div'
will put <button id="openEssay">
inside this.el
so only (1) will call openEssay
when you hit the button. If you put your button inside #div
then (2) would also work.
这篇关于Click事件中的“查看Backbone.js的”不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!