jquery – 有没有办法结合$(this)与:nth-​​child?

我正在一个一个迭代的中间,想要呼出第二或第三个孩子的每一个,但不能使它的工作。
alert($(this + ' :nth-child(2)').attr('id'));

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

$(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'...

解决方法

您需要的是 context.对于上下文,选择器将仅查找作为上下文的子元素(在本例中为此)的元素。
$(':nth-child(2)',this).attr('id');

jsFiddle Demo

这基本上是一样的:

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

如果你只需要直接的孩子,而不是每一个后裔,你应该使用.children():

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

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...