我有一个类似的布局:

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

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

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

$(this)

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


当前回答

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

1 find():

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

2个孩子():

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

其他回答

尝试以下代码:

$(this).children()[0]

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

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

如果你需要得到第一个正好向下一级的img,你可以这样做

$(this).children("img:first")

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

1 find():

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

2个孩子():

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

使用本机javascript,您可以使用

如果您有多个图像标记,请使用

this.querySelectorAll(“img”)

如果只有一个图像标签,那么我们

this.querySelector(“img”)