我是 Backbone 新人。有a link for my code.
$(function (){
var test = {};
test.View = Backbone.View.extend({
el: 'body',
initialize: function (){
this.input = $('#new-todo');
},
events: {
'keyup #new-todo': 'check'
},
check: function (){
console.log('!');
$('#test').html(this.input.val());
}
});
test.view = new test.View();
});
我找到了a similar question但仍然很困惑。当我设置el:'#container'
时keyup
事件不起作用。
我想知道,View.el
是什么? ,它的作用是什么?与 el: 'body'
有什么不同和el: '#container'
.
最佳答案
Backbone 期望 View 仅处理其特定 DOM 元素 (el) 内的事件。您的 #new-todo
位于 #container
I want to know, what is it the View.el, what's is that its function? What's the different from el: 'body' and el: '#container'.
el 是 View 始终引用的元素。事件附加到此 el 元素内的元素。
这样想: View 仅引用 el 元素。
关于javascript - 为什么主干 view.el 是 'body' 一切正常,但当 view.el 是 '#container' 时就坏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31098461/