我有一个类似的布局:

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

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

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

$(this)

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


当前回答

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

$(this).next();

其他回答

直接子女是

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

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

jQuery("img", this);

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

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

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

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

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

$(this).next();

此外,这也应该起作用:

$("#id img")

使用本机javascript,您可以使用

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

this.querySelectorAll(“img”)

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

this.querySelector(“img”)