javascript - 如何将第 n 个子选择器与 jquery 子选择器一起使用?

标签 javascript jquery children

这工作正常:

$("#element").find("> tr > td > i > a")

但我试图使用 nth-child 选择器来表示我想要一个特定编号的子项。例如,我想要:

$("#element").find("> tr > td > i > a:nth-child(3)")

我没有得到任何结果。有谁知道我如何选择所有第 n 个编号的 child ?预先感谢!

最佳答案

首先,除非非常需要直接后代( > )选择器,否则我认为不需要包含它们,您也可以适当缩短 CSS 选择器:

//Select 3rd <a> tag, within the context of #element td
$('a:nth-child(3)', '#element td');
   //or
$('#element td a:nth-child(3)')

fiddle :http://jsfiddle.net/KGWuK/

不过,目前为您提供的问题选择器仅在您的 HTML structure 时才有效。是:

 <td>
     <i>
         <a>One</a>
         <a>Two</a>
         <a>Three</a>
     </i>
 </td>    

这不是一种非常可取的标记样式。那是因为原始选择器选择 <a> <i>直接子代的标签

关于javascript - 如何将第 n 个子选择器与 jquery 子选择器一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989784/

相关文章:

javascript - 使用 JQuery .not( ) 淡出除所选内容及其子元素之外的所有 div

javascript - 返回数组和子节点

javascript - PHP乘法和减法脚本

javascript - AngularJS : Scope not shared across directives

javascript - jQuery "return { foo: bar, foo2: bar2 }"- 这是什么?

javascript - 获取没有 id 的 HTML 中特定段落的链接

python-3.x - Bokeh 布局 : How to remove children

javascript - jQuery post() JSP 返回集合

javascript - 在页面调用时立即调用该函数

javascript - 如何在 PHP 中将动态文本附加到 url 中?