简体   繁体   English

backbone.js和el属性

[英]backbone.js and el attribute

(function ($) {
window.AppView = Backbone.View.extend({
  el: $("body"),
  events: {
    "click #add-friend":  "showPrompt",
  },
  showPrompt: function () {
    var friend_name = prompt("Who is your friend?");
  }
});
var appview = new AppView;
})(jQuery);
  1. Can anyone explain me what is el here. 谁能给我解释一下什么是el这里。 Is it element? 它是元素吗?
  2. Does the el argument accept object, if so can i pass my custom view object where my button or elements need to be added... el参数是否接受对象,如果是这样,我可以传递我的自定义视图对象,其中我的按钮或元素需要添加...
  1. Yes it's a DOM element. 是的,它是一个DOM元素。
  2. No you cannot pass a custom object. 不,你不能传递自定义对象。 You either specify an existing element, or creating one from the tagName , className , id and attributes properties of the view. 您可以指定现有元素,也可以从视图的tagNameclassNameidattributes属性创建一个元素。 If you don't specify an element, it defaults to a div 如果未指定元素,则默认为div

It's all in the official documentation actually... 这实际上是在官方文档中 ......

Alladnian answered it but I would add that when using el you can make use of $el which is a cached jQuery object of your view element. Alladnian回答了这个问题,但我想补充一点,当你使用el你可以使用$el ,它是你视图元素的缓存jQuery对象。

So you can always simply pass only the tag you wish to use (for consistency, brevity and flexibility) and then reference it as $el to make use of it as a jQuery object. 因此,您始终只需传递您希望使用的标记(为了一致性,简洁性和灵活性),然后将其作为$el引用,以将其用作jQuery对象。

this.$el.addClass("active");

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

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