对于给定的例子,this.el&之间有什么区别?this.$el
我理解this.$el指向jQuery对象this.el,在这种情况下是'li'.
当我渲染视图时,我可以选择this.el,或者this.$el.当我引用jQuery对象时,如何向页面呈现内容?我可以看到如何使用this.$el.html(property),指向html,但为什么追加this.$el和渲染是让我困惑的原因.
var Person = Backbone.Model.extend({
defaults: {
name: 'John Doe',
age: 30,
occupation: 'worker'
}
});
var PersonView = Backbone.View.extend({
tagName: 'li',
initialize: function() {
this.render();
},
render: function() {
var out = this.$el.html(this.model.get('name') + this.model.get('occupation'));
console.log(this, this.el, this.$el, out);
$('body').append(this.$el);
},
});
var person = new Person;
var personview = new PersonView({model: person});
Run Code Online (Sandbox Code Playgroud)