javascript - jQuery 中此对象的目标子元素?

标签 javascript jquery this

如何从 $(this) 获取子元素?

例如,我如何定位 this 对象中的 span 元素?

最佳答案

您可以使用find方法:

$(this).find("span");

这将找到作为 this 引用的元素的后代的所有 span 元素。

如果你只关心直系 child ,你可以使用 children相反:

$(this).children("span");

或者,您可以使用 this 作为选择器的上下文:

$("span", this);

如果 this 是包含一组同级元素的 jQuery 对象(因此 span 不是后代),则需要另一种解决方案。在这种情况下,您需要 filter :

$(this).filter("span");

关于javascript - jQuery 中此对象的目标子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8857848/

相关文章:

javascript - 实时更新标记,无需加载网页,从文本文件加载更新的坐标 Google map Api

jquery - Struts2-Jquery Grid CSS 编辑

javascript - 捆绑代码后 safari 出现语法错误

javascript - 如何(最好)在作为回调传递的对象方法中访问 'this'

javascript - 在构造函数中使用 'this' 关键字是否会增加总体内存使用量?

c++ - 在 C++ 类方法中使用 *this 完全覆盖自实例化

javascript - 制作一键启动功能,一键停止功能

javascript - NavigatorView的页面堆栈为空返回主页Onsen UI

javascript - 检查页面重新加载时的 JS 变量并相应地隐藏/显示 div

javascript - WordPress 主题 : add custom scripts and jquery the correct way