我有一个类似的布局:

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

并希望使用jQuery选择器在单击时选择div中的子img。

为了得到div,我有一个选择器:

$(this)

如何使用选择器获取子img?


当前回答

不知道DIV的ID,我想您可以这样选择IMG:

$("#"+$(this).attr("id")+" img:first")

其他回答

直接子女是

$('> .child-class', this)

你可以使用

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
 $(this).find('img');
</script>

jQuery构造函数接受第二个名为context的参数,该参数可用于重写选择的上下文。

jQuery("img", this);

这与像这样使用.find()相同:

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

如果所需的img只是所单击元素的直接后代,则还可以使用.children():

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

此外,这也应该起作用:

$("#id img")

不知道DIV的ID,我想您可以这样选择IMG:

$("#"+$(this).attr("id")+" img:first")