本文介绍了有没有办法将$(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
发布评论