[英]How can I get a specific number child using CSS?
I have a table
whose td
s are created dynamically. 我有一个
table
其td
是动态创建的。 I know how to get the first and last child but my question is: 我知道如何生第一个和最后一个孩子,但我的问题是:
Is there a way of getting the second or third child using CSS? 有没有办法让第二个或第三个孩子使用CSS?
For modern browsers, use td:nth-child(2)
for the second td
, and td:nth-child(3)
for the third. 对于现代浏览器,第二个
td
使用td:nth-child(2)
,第三个使用td:nth-child(3)
。 Remember that these retrieve the second and third td
for every row . 记住,它们检索每行的第二和第三
td
。
If you need compatibility with IE older than version 9, use sibling combinators or JavaScript as suggested by Tim . 如果需要与版本9之前的IE兼容,请使用Tim推荐的同级组合器或JavaScript。 Also see my answer to this related question for an explanation and illustration of his method.
另请参阅我对此相关问题的回答,以获取有关其方法的解释和说明。
For IE 7 & 8 (and other browsers without CSS3 support not including IE6) you can use the following to get the 2nd and 3rd children: 对于IE 7和8(以及其他不支持CSS3的浏览器(不包括IE6)),您可以使用以下命令获取第二个和第三个子代:
2nd Child: 第二个孩子:
td:first-child + td
3rd Child: 第三个孩子:
td:first-child + td + td
Then simply add another + td
for each additional child you wish to select. 然后,只需为要选择的每个其他孩子添加另一个
+ td
。
If you want to support IE6 that can be done too! 如果您想支持IE6,也可以这样做! You simply need to use a little javascript (jQuery in this example):
您只需要使用一些javascript(在此示例中为jQuery):
$(function() {
$('td:first-child').addClass("firstChild");
$(".table-class tr").each(function() {
$(this).find('td:eq(1)').addClass("secondChild");
$(this).find('td:eq(2)').addClass("thirdChild");
});
});
Then in your css you simply use those class selectors to make whatever changes you like: 然后在CSS中,您只需使用这些类选择器即可进行所需的任何更改:
table td.firstChild { /*stuff here*/ }
table td.secondChild { /*stuff to apply to second td in each row*/ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.