nth-child和子代选择器未选择所有期望的元素 [英] nth-child and descendant selector not selecting all expected elements
本文介绍了nth-child和子代选择器未选择所有期望的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的DOM如下: http://jsfiddle.net/pimvdb/AHJXk/1/.
<table>
<tr>
<td>
<input type="text"><input type="text">
</td>
<td>
<input type="text"><input type="text">
</td>
</tr>
<tr>
<td>
<input type="text"><input type="text">
</td>
<td>
<input type="text"><input type="text">
</td>
</tr>
</table>
我正在尝试选择每个tr
的第二个td
中的所有input
,即总共四个.我认为以下选择器会起作用:
I'm trying to select all input
s in the second td
of each tr
, i.e. four in total. I thought the following selector would work:
$('table tr td:nth-child(2) input')
但是它仅返回第二个td
的 first input
(总共两个).这是为什么?如果我这样做:
But it only returns the first input
of each second td
(two in total). Why is that? If I do:
$('table tr td:nth-child(1) input')
那么我确实确实得到了每个第一个td
(总共四个)的全部 input
.
then I do indeed get all input
s of each first td
(four in total).
那么:nth-child(2)
为什么不返回所有input
而是仅返回每个匹配的td
的第一个?
So why is :nth-child(2)
not returning all input
s but only the first one of each matched td
?
推荐答案
这将使您全部四个:
$('input','table tr td:nth-child(2)')
小提琴: http://jsfiddle.net/AHJXk/3/
这篇关于nth-child和子代选择器未选择所有期望的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文