Can you please tell the difference between $el
and el
in Backbone.js views?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
It is so late to answer it but -->
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
is the native DOM element, untouched by jQuery.mu is too short is exactly right:
And it's easy to understand why, look at the view's
_setElement
function:This ensures that
el
is always a DOM element, and that$el
is always a jQuery object of it. So the following is valid even though I used a jQuery object as theel
option or property:What is a cached jQuery object?
It's a jQuery object kept inside a variable for reuse purpose. It avoids the costly operation of finding the element with something like
$(selector)
each time.Here's an example:
See an extensive answer I wrote to know more.
lets say that you do this
with this
one is the html element and the other is the jQuery object of the element.
In short, el gives you access to HTML DOM elements, i.e you can refer and access them, whereas $el is jQuery wrapper around el.
$el not only provides access to particular DOM element, moreover it acts as a jQuery selector and you have privilege to use jQuery library functions like show(), hide(), etc on the particular DOM element.