javascript - What does this.$('.selector') do in jQuery? - Stack Overflow

I see this in someone's code: this.$('.selector') and am curious what that does. "t

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?

Share Improve this question edited Sep 28, 2012 at 16:24 j08691 208k32 gold badges269 silver badges280 bronze badges asked Sep 28, 2012 at 16:22 Chad JohnsonChad Johnson 21.9k36 gold badges116 silver badges218 bronze badges 1
  • 1 It's a shortcut for $(this.el).find(...) or $('.selector',this.el) - to scope the queries w.r.t. to the el element – PhD Commented Sep 30, 2012 at 0:00
Add a ment  | 

3 Answers 3

Reset to default 9

From 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745399606a4626039.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信