简体   繁体   English

Backbone this.el vs this。$ el

[英]Backbone this.el vs this.$el

For the given example, what is the difference between this.el & this.$el 对于给定的例子, this.elthis.$el之间有什么区别this.$el

I understand that this.$el points to the jQuery object of this.el , which in this case is 'li' . 我明白this.$el指向this.el的jQuery对象,在这种情况下是'li'

When I render a view, I can choose between this.el , or this.$el . 当我渲染一个视图时,我可以选择this.el或者this.$el How am I able to render something to a page when I reference the jQuery object? 当我引用jQuery对象时,如何向页面呈现内容? I can see how using this.$el.html(property) , points to the html, but why appending this.$el and having it render is what confuses me. 我可以看到如何使用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});

Bojangles is correct. Bojangles是正确的。

this.$el is a reference to the element in the context of jQuery, typically for use with things like .html() or .addClass() , etc. For example, if you had a div with id someDiv , and you set it to the el property of the Backbone view, the following statements are identical: this.$el .addClass() this.$el是对jQuery上下文中元素的引用,通常用于.html().addClass()类的东西等。例如,如果你有一个id为someDiv的div,你设置它对于Backbone视图的el属性,以下语句是相同的:

this.$el.html() $("#someDiv").html() $(this.el).html() this.$el.html() $("#someDiv").html() $(this.el).html()

this.el is the native DOM element, untouched by jQuery. this.el是本机DOM元素,不受jQuery的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM