骨干this.el VS这一点。$埃尔 [英] Backbone this.el vs this.$el

查看:134
本文介绍了骨干this.el VS这一点。$埃尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定的例子,之间有什么区别 this.el &安培; 这一点。$埃尔

据我所知,这一点。$埃尔 this.el 的jQuery对象,在这种情况下,为

当我渲染一个观点,我可以 this.el 之间进行选择,或这一点。$埃尔。我怎么能够呈现的东西的网页,当我引用jQuery对象?我可以看到如何使用这一点。$ el.html(财产),指向HTML,但为什么追加这一点。$埃尔并让它呈现是混淆了我。

 变种人= Backbone.Model.extend({
    默认值:{
        名称:'李四',
        年龄:30
        职业:工人
    }
});VAR PersonView = Backbone.View.extend({
    标签名:礼,    初始化:功能(){
        this.render();
    },    渲染:功能(){
        变量超出= $这个el.html(this.model.get('名')+ this.model.get('占领'));
        的console.log(这一点,this.el,这$埃尔,出);
        $('身体')追加(这$ EL)。
    },});变种人=新的人;
VAR personview =新PersonView({模式:人});


解决方案

Bojangles是正确的。

这一点。$埃尔是元素的引用jQuery中的背景下,对于一般的东西)使用像的.html( .addClass()等。例如,如果您有ID的div someDiv ,并且将它设置为骨干视图的属性,下面的语句是相同的:

这一点。$ el.html()
$(#someDiv)。HTML()
$(this.el)的.html()

this.el 是本机的DOM元素,的jQuery不变。

For the given example, what is the difference between this.el & this.$el

I understand that this.$el points to the jQuery object of this.el , which in this case is 'li'.

When I render a view, I can choose between this.el, or this.$el. How am I able to render something to a page when I reference the jQuery object? 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.

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.

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.html() $("#someDiv").html() $(this.el).html()

this.el is the native DOM element, untouched by jQuery.

这篇关于骨干this.el VS这一点。$埃尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆