如何为骨干视图设置Parent元素?
var TodosView = Backbone.View.extend({
tagName: 'p', // required, but defaults to 'div' if not set
className: 'container', // optional, you can assign multiple classes to
// this property like so: 'container homepage'
id: 'todos', // optional
initialize: function(){
// debugger
this.$el.html("bamboo4a")
$("body").append(this.$el);
}
});
var todosView = new TodosView();
我不想写$(" body")。追加
答案 0 :(得分:2)
对于主视图,您可以在创建视图对象时通过将选项传递给其构造函数(即选项el
)来设置视图元素。
var MyView = Backbone.View.extend({
template: '<p>Hello World!</p>',
render: function() {
this.$el.html(this.template);
}
});
new MyView({
el: 'body' // or el: '#content' and so on
}).render();
<强> Documentation 强>
<强> Demo 强>