Javascript Backbone.js:未呈现视图
我有一个非常基本的主干视图,我正在尝试渲染,但由于某些原因,屏幕上没有显示任何内容。代码是:Javascript Backbone.js:未呈现视图,javascript,backbone.js,Javascript,Backbone.js,我有一个非常基本的主干视图,我正在尝试渲染,但由于某些原因,屏幕上没有显示任何内容。代码是: var UserList = Backbone.View.extend({ tagName: 'ul', id: 'user-list', initialize: function () { _.bindAll(this, 'render'); }, render: function () { console.log(this.$e
var UserList = Backbone.View.extend({
tagName: 'ul',
id: 'user-list',
initialize: function () {
_.bindAll(this, 'render');
},
render: function () {
console.log(this.$el.html()); // LOG #1
this.$el.html('<li>Is this showing up?</li>');
console.log(this.$el.html()); // LOG #2
}
});
var userList = new UserList();
userList.render();
var UserList=Backbone.View.extend({
标记名:“ul”,
id:'用户列表',
初始化:函数(){
_.bindAll(这是“呈现”);
},
渲染:函数(){
console.log(this.$el.html());//log#1
这是。$el.html(“显示了吗? ”);
console.log(this.$el.html());//log#2
}
});
var userList=new userList();
render();
第一次记录
el
的内容时,它没有显示任何内容,第二次它有正确的内容,但屏幕上没有显示任何内容。有人能想到为什么会发生这种情况吗?我知道渲染函数正在启动,并且this
已正确绑定到this.render
。另外,this.$el
被设置为有效的jQuery对象(在我的HTML中定义)。由于您没有在视图上指定el
,因此它不会附加到DOM的任何位置。您需要显式附加它:
var userList = new UserList();
$("body").html(userList.render());
并从渲染
返回一个值:
render: function () {
console.log(this.$el.html()); // LOG #1
this.$el.html('<li>Is this showing up?</li>');
console.log(this.$el.html()); // LOG #2
return this.$el;
}
render:function(){
console.log(this.$el.html());//log#1
这是。$el.html(“显示了吗? ”);
console.log(this.$el.html());//log#2
归还这个。$el;
}
它似乎出现在第二个控制台调用b/c中,您正在明确设置该值。似乎init函数根本没有设置el的值。如果我从
render
记录this.el
,我确实得到了正确的DOM元素。。。这就是你的意思吗?也许我太笨了,谢谢!我想这就足够指定ID了。。。吸取的教训。