我有一个类似的布局:

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

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

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

$(this)

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


当前回答

直接子女是

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

其他回答

直接子女是

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

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

jQuery("img", this);

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

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

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

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

如果DIV标记后面紧跟IMG标记,则还可以使用:

$(this).next();

尝试以下代码:

$(this).children()[0]

你可以使用

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