如何获得$(this)选择器的子节点? [英] How to get the children of the $(this) selector?

查看:176
本文介绍了如何获得$(this)选择器的子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局类似于:

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

并且想使用jQuery选择器来选择子 img div 内的code>。

and would like to use a jQuery selector to select the child img inside the div on click.

获取 div ,我有这个选择器:

To get the div, I've got this selector:

$(this)

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

How can I get the child img using a selector?

推荐答案

jQuery构造函数接受名为 context 可用于覆盖选择的上下文。

The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.

jQuery("img", this);

这与使用 .find() ,如下所示:

Which is the same as using .find() like this:

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

如果你想要的imgs只是 点击元素的直接后代,您也可以使用 .children()

If the imgs you desire are only direct descendants of the clicked element, you can also use .children():

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

这篇关于如何获得$(this)选择器的子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆