有没有办法将$(this)与:nth

编程入门 行业动态 更新时间:2024-10-27 23:30:34
本文介绍了有没有办法将$(this)与:nth-​​child结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正处于.each迭代的中间,想为每个子项召集第二个或第三个孩子.但是无法使其工作.

I'm in the middle of an .each iteration and wanted to call out the 2nd or 3rd child for the each..but cant make it work.

alert($(this + ' :nth-child(2)').attr('id'));

我能想到的唯一选择就是像这样的可怕愚蠢:

My only option that I can think of is something terrible goofy like this:

$(this).children(':first').next().attr('id', 'ddParam' + newCount); $(this).children(':first').next().next().attr('id', 'txt' + newCount); $(this).children(':first').next().next().next().attr('id'...

推荐答案

您需要的是上下文.对于上下文,选择器将仅查找作为上下文子级的元素(在本例中为this).

What you need is context. With context, the selector will only look for elements that are the children of the context (in this case this).

$(':nth-child(2)', this).attr('id');

jsFiddle演示

这基本上与:

$(this).find(':nth-child(2)').attr('id');

如果只需要直系子代,而不是每个后代,则应使用.children():

If you only need the direct children, not every descendant, you should use .children():

$(this).children(':nth-child(2)').attr('id');

更多推荐

有没有办法将$(this)与:nth

本文发布于:2023-10-31 01:27:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   nth

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!