admin管理员组文章数量:1426066
I see this in someone's code: this.$('.selector')
and am curious what that does. "this" is a Backbone view. So what does prefixing "this." onto a jQuery selector, in the given context, do?
I see this in someone's code: this.$('.selector')
and am curious what that does. "this" is a Backbone view. So what does prefixing "this." onto a jQuery selector, in the given context, do?
-
1
It's a shortcut for
$(this.el).find(...)
or$('.selector',this.el)
- to scope the queries w.r.t. to theel
element – PhD Commented Sep 30, 2012 at 0:00
3 Answers
Reset to default 9From the doc:
$ (jQuery or Zepto)view.$(selector)
If jQuery or Zepto is included on the page, each view has a $ function that runs queries scoped within the view's element. If you use this scoped jQuery function, you don't have to use model ids as part of your query to pull out specific elements in a list, and can rely much more on HTML class attributes. It's equivalent to running:
view.$el.find(selector)
ui.Chapter = Backbone.View.extend({
serialize : function() {
return {
title: this.$(".title").text(),
start: this.$(".start-page").text(),
end: this.$(".end-page").text()
};
}
});
In short, it's used to access some elements of View with a familiar syntax.
It is basically limiting the search for elements with a class of selector
to the element your View is based off of.
It's basically changing the scope of the search from document
to this
, which is obviously some element.
本文标签: javascriptWhat does this(39selector39) do in jQueryStack Overflow
版权声明:本文标题:javascript - What does this.$('.selector') do in jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745399605a2656975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论