How to get the children of the $(this) selector?(如何获取 $(this) 选择器的孩子?)
问题描述
我有一个类似这样的布局:
I have a layout similar to this:
<div id="..."><img src="..."></div>
并想使用 jQuery 选择器在点击时选择 div
内的子 img
.
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是only被点击元素的直接后代,你也可以使用.children()
:
If the imgs you desire are only direct descendants of the clicked element, you can also use .children()
:
jQuery(this).children("img");
这篇关于如何获取 $(this) 选择器的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取 $(this) 选择器的孩子?


基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01