jQuery first child of quot;thisquot;(“this的jQuery第一个孩子)
问题描述
我正在尝试将this"从单击的范围传递给 jQuery 函数,然后该函数可以在该单击元素的第一个子元素上执行 jQuery.好像没弄好...
I'm trying to pass "this" from a clicked span to a jQuery function that can then execute jQuery on that clicked element's first child. Can't seem to get it right...
<p onclick="toggleSection($(this));"><span class="redClass"></span></p>
Javascript:
Javascript:
function toggleSection(element) {
element.toggleClass("redClass");
}
如何引用元素的 :first-child?
How do I reference the :first-child of element?
推荐答案
如果您想将选择器应用到现有 jQuery 集提供的上下文,请尝试 find() 函数:
If you want to apply a selector to the context provided by an existing jQuery set, try the find() function:
element.find(">:first-child").toggleClass("redClass");
Jørn Schou-Rode 指出,您可能只想找到上下文元素的第一个 直接后代,因此 子选择器 (>).他还指出你也可以使用 children() 函数,它与 find() 非常相似,但只搜索层次结构深处的一层(即所有你需要的......):
Jørn Schou-Rode noted that you probably only want to find the first direct descendant of the context element, hence the child selector (>). He also points out that you could just as well use the children() function, which is very similar to find() but only searches one level deep in the hierarchy (which is all you need...):
element.children(":first").toggleClass("redClass");
这篇关于“this"的jQuery第一个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“this"的jQuery第一个孩子
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
