Html 需要使用 CSS 选择器选择第二个表的第二行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17633959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:05:45  来源:igfitidea点击:

Need to select second table's second row using CSS selectors

htmlcss

提问by Arup Rakshit

I have an HTMLtable as below :

我有一个HTML表,如下所示:

<table class="reference ">
  <tr>..</tr>
     .
     .
  <tr>..</tr>
</table>
<table class="reference ">
  <tr>..</tr>
     .
     .
  <tr>..</tr>
</table>
<table class="reference ">
  <tr>..</tr>
     .
     .
  <tr>..</tr>
</table>

I know the XPATH: //table[@class = 'reference '][2]/tr[2]

我知道XPATH//table[@class = 'reference '][2]/tr[2]

I want to select second table's second row. Can any one help how to write the CSS selectorsfrom the same?

我想选择第二个表的第二行。任何人都可以帮助如何从相同的地方编写CSS 选择器吗?

回答by dsgriffin

"I know the XPATH : //table[@class = 'reference '][2]/tr[2]

I want to select second table's second row. Can any one help how to write the CSS selectors from the same?"

“我知道 XPATH : //table[@class = 'reference '][2]/tr[2]

我想选择第二个表的第二行。任何人都可以帮助如何从相同的地方编写 CSS 选择器吗?”

So, you mean like:

所以,你的意思是:

table.reference.:nth-child(2) tr:nth-child(2)

This selects the second descendant trelement of the second table which has both classes (referenceand ).

这将选择tr具有两个类 (reference)的第二个表的第二个后代元素。

jsFiddle here.

jsFiddle 在这里

回答by Nikola Mitev

table.reference:nth-of-type(2) tr:nth-child(2)
{
    background-color:red;
}

回答by Sachin

Try this

尝试这个

table.reference:nth-child(2) tr:nth-child(2)
{
    background-color:red;
}

Js Fiddle

Js小提琴