如何获取 $(this) 选择器的孩子?
我有一个类似这样的布局:
I have a layout similar to this:
<div id="..."><img src="..."></div>
并想使用 jQuery 选择器在点击时选择 div 内的子 img.
and would like to use a jQuery selector to select the child img inside the div on click.
要获得 div,我有这个选择器:
To get the div, I've got this selector:
$(this)
如何使用选择器获取子 img?
How can I get the child img using a selector?
推荐答案
jQuery 构造函数接受名为 context 可用于覆盖选择的上下文.
The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.
jQuery("img", this);
这与像这样使用 .find() 相同:
Which is the same as using .find() like this:
jQuery(this).find("img");
如果你想要的imgs是only被点击元素的直接后代,你也可以使用.children():
If the imgs you desire are only direct descendants of the clicked element, you can also use .children():
jQuery(this).children("img");
相关文章