简体   繁体   中英

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. 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...
  1. Yes it's a DOM element.
  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. If you don't specify an element, it defaults to a 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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