我有一个类似的布局:

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

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

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

$(this)

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


当前回答

此外,这也应该起作用:

$("#id img")

其他回答

尝试以下代码:

$(this).children()[0]

jQuery的每个都是一个选项:

<div id="test">
    <img src="testing.png"/>
    <img src="testing1.png"/>
</div>

$('#test img').each(function(){
    console.log($(this).attr('src'));
});

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

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

您可以使用以下任一方法:

1 find():

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

2个孩子():

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

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

$(this).next();