让 eventDelegate 与backbone.js 一起工作
我有以下 js:
SwitcherView = Backbone.View.extend({
events: {
'click li' : 'clickLi',
'click' : 'click'
},
initialize: function(){
this.liEl = this.$('li');
console.log(this.liEl);
},
clickLi: function(event){
console.log('click', event);
},
click: function(event){
console.log('click', event);
}
});
new SwitcherView({el: $('#viewswitcher')});
和以下 HTML:
<div id="viewswitcher" class="block block-nav">
<div class="block-title">
<strong><span>Uw kozijn</span></strong>
</div>
<div class="block-content">
<ul class="items">
<li id="designer-container" class="ui-state-active ui-state-default">
<span class="ui-icon ui-icon-designer"></span> Ontwerper
</li>
<li id="options-container">
<span class="ui-icon ui-icon-options"></span> Kleuropties
</li>
<li id="notes-container">
<span class="ui-icon ui-icon-notes"></span> Aantekeningen
</li>
</ul>
</div>
</div>
问题是“click”事件确实有效,但我无法让“click li”工作。
我正在运行backbone.js 0.5.3、underscore.js 1.2.1 和jQuery 1.6.4。
I have the following js:
SwitcherView = Backbone.View.extend({
events: {
'click li' : 'clickLi',
'click' : 'click'
},
initialize: function(){
this.liEl = this.$('li');
console.log(this.liEl);
},
clickLi: function(event){
console.log('click', event);
},
click: function(event){
console.log('click', event);
}
});
new SwitcherView({el: $('#viewswitcher')});
And the following HTML:
<div id="viewswitcher" class="block block-nav">
<div class="block-title">
<strong><span>Uw kozijn</span></strong>
</div>
<div class="block-content">
<ul class="items">
<li id="designer-container" class="ui-state-active ui-state-default">
<span class="ui-icon ui-icon-designer"></span> Ontwerper
</li>
<li id="options-container">
<span class="ui-icon ui-icon-options"></span> Kleuropties
</li>
<li id="notes-container">
<span class="ui-icon ui-icon-notes"></span> Aantekeningen
</li>
</ul>
</div>
</div>
The problem is that the 'click' event does work, but I cant get 'click li' working.
I'm running backbone.js 0.5.3, underscore.js 1.2.1 and jQuery 1.6.4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我在 jsFiddle 中尝试你的代码时,它工作得很好。
愚蠢的问题:您是否通过单击第一个行项目(Uw kozijn)来测试它?因为如果是的话,它就不在列表中......它在它自己的 DIV 中,所以你不会得到这两个事件。但是,如果您单击其他任何内容,一切都会按我的预期进行。
When I try your code in jsFiddle, it works just fine.
Dumb question: Are you testing it by clicking on the first line item (Uw kozijn)? Because if you are, it is not in the list... it is in it's own DIV so you will not get both events. If you click on anything else, however, it all works as I would expect.
问题是一个旧的 jQuery 验证库,更新该库后,一切都按预期工作!
The problem was an old jQuery validate library, after updating that library, everything worked as expected!