相关疑难解决方法(0)

Backbone this.el vs this.$ el

对于给定的例子,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)

javascript jquery backbone.js

3
推荐指数
1
解决办法
4992
查看次数

标签 统计

backbone.js ×1

javascript ×1

jquery ×1