如何获得 $(this) 选择器的孩子? [英] How to get the children of the $(this) selector?
本文介绍了如何获得 $(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 仅被点击元素的直接后代,您还可以使用 .children()
:
If the imgs you desire are only direct descendants of the clicked element, you can also use .children()
:
jQuery(this).children("img");
这篇关于如何获得 $(this) 选择器的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文