BackboneJs:在视图中,el:和tagName之间有什么区别? [英] BackboneJs: In a view whats the difference between el: and tagName:

查看:113
本文介绍了BackboneJs:在视图中,el:和tagName之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力围绕这个概念.

I'm trying to wrap my head around this concept.

您能为我做些愚蠢的事,也许提供一个简单示例说明el:属性和tagName:属性之间的区别吗?

Can you dumb this down for me and maybe provide a simple example of the difference between the el: attribute and the tagName: attribute?

在某些示例中,不同的视图有时使用el:,而其他视图则使用tagName:.

In some examples, different views use el: sometimes and others use tagName:.

我专门弄乱了我自己的示例

I'm specifically messing around with my own implementation of this example

推荐答案

区别是:

el应该用于保留对表示整个视图的实际DOM节点的引用.

el should be used to preserve a reference to the actual DOM node representing the view as a whole.

这意味着您可以使用jQuery或w/e轻松地对其执行操作. $(this.el).hide()或$(this.el).html('我现在是一个Jquery对象');

This means you can easily perform actions on it with jQuery or w/e. $(this.el).hide() or $(this.el).html('I am a Jquery Object now');

TagName仅是一个字符串,用于确定DOM节点el的类型.默认值为div,但如果需要,可以将其设为任何HTML元素.

TagName is only a string that is used to determine the type of DOM node el will be. The default is div, but if you wanted, you could make it any HTML element you please.

考虑:

var view = Backbone.View.extend({
    tagName: 'p',
    initialize: function () {
        _.bindAll(this, 'render');
    },
    render: function() {
        $(this.el).html('I am a jQuery-ized paragraph');
        return this;
    }
});


$(document.body).append(new view().render().el);

您可能会遇到的问题是,有时会在视图的实例化上设置el,在这种情况下,tagName无关紧要:

The problem you might be running into is that sometimes you set el on the instantiation of a view, in which case the tagName is irrelevant:

var myView = new view({ el: $("someExistingEl") });

这篇关于BackboneJs:在视图中,el:和tagName之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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