admin管理员组文章数量:1405131
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});
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});
Share
edited Jan 19, 2016 at 2:04
Irwin
12.8k12 gold badges72 silver badges97 bronze badges
asked Mar 10, 2014 at 15:17
seasickseasick
1,2452 gold badges17 silver badges29 bronze badges
2
-
1
If you need jQuery methods or the cross browser support it brings, use
this.$el
, otherwise use the native DOM methods withthis.el
– Bojangles Commented Mar 10, 2014 at 15:19 - 1 Possible duplicate of What's the difference between: $(this.el).html and this.$el.html – pd12 Commented Jan 19, 2016 at 1:58
1 Answer
Reset to default 5Bojangles 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.
本文标签: javascriptBackbone thisel vs thiselStack Overflow
版权声明:本文标题:javascript - Backbone this.el vs this.$el - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744883881a2630368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论