How to get the children of the $(this) selector?

I have a layout similar to this:

<div id="..."><img src="..."></div>

and would like to use a jQuery selector to select the child img inside the div on click.

To get the div , I've got this selector:

$(this)

How can I get the child img using a selector?


The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.

jQuery("img", this);

Which is the same as using .find() like this:

jQuery(this).find("img");

If the imgs you desire are only direct descendants of the clicked element, you can also use .children() :

jQuery(this).children("img");

You could also use

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

which would return all img s that are descendants of the div


如果您需要将第一个img降到一个级别,您可以这样做

$(this).children("img:first")
链接地址: http://www.djcxy.com/p/694.html

上一篇: 了解Python的切片符号

下一篇: 如何获得$(this)选择器的孩子?