有没有办法将$(this)与:nth-child结合? [英] Is there a way to combine $(this) with :nth-child?
本文介绍了有没有办法将$(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'...
推荐答案
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');
这基本上与:
$(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-child结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文