简体   繁体   English

如何在Backbone.js中设置View的id?

[英]How to set the id of a View in Backbone.js?

In Backbone.js when I create a view I can initialise the 'class' attribute of it's element as shown below: 在Backbone.js中,当我创建一个视图时,我可以初始化它的元素的'class'属性,如下所示:

var newView = Backbone.View.extend({
    className: 'foo'
});

Is there a similar way to set the 'id' attribute in a similar way? 是否有类似的方式以类似的方式设置'id'属性?

var newView = Backbone.View.extend({
    id: 'foo1',  // or
    tagName: 'foo2', // or ..
    className: 'foo3' // 
});

Hope that helps! 希望有所帮助!

There is an existing id property you can use. 您可以使用现有的id属性。 Here's a relevant portion from the docs: 以下是文档的相关部分:

elview.el All views have a DOM element at all times (the el property), whether they've already been inserted into the page or not. elview.el所有视图始终都有一个DOM元素(el属性),无论它们是否已经插入到页面中。 In this fashion, views can be rendered at any time, and inserted into the DOM all at once, in order to get high-performance UI rendering with as few reflows and repaints as possible. 通过这种方式,可以随时呈现视图,并一次性将所有视图插入到DOM中,以便通过尽可能少的回流和重绘来获得高性能的UI呈现。 this.el is created from the view's tagName, className, id and attributes properties, if specified. this.el是根据视图的tagName,className,id和attributes属性创建的(如果已指定)。 If not, el is an empty div. 如果没有,el是一个空div。

Also, you can bind to an existing element that is already in the HTML if that works better for you. 此外,如果更适合您,您可以绑定到HTML中已有的元素。

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

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