jQuery $(this) syntax question

流过昼夜 提交于 2019-12-06 15:45:06

Use

 $(this).find("childrenSelector")

This may be what you are looking for:

$('childElement', $(this))

Basically it will search for childElement within the context of the current element, this.


For more information, see the documentation for the jQuery() function. In particular, the following excerpt explains the second argument and how it is equivalent to using find:

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, if within a callback function we wish to do a search for an element, we can restrict that search:

$('div.foo').click(function() {
  $('span', this).addClass('bar');
});

Since we've restricted the span selector to the context of this, only spans within the clicked element will get the additional class.

Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span').

$(this).find('childrenSelector');

Or...

$(this).children('.element');

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!