如何选择这个td元素?

eas*_*ndr 1 css css-selectors css3

我需要选择值为2的td元素.我怎么能用纯CSS做到这一点?

<tr class="Foobar">
   <td>1</td>
   <td>2</td>
   <td>3</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

谢谢!

Dav*_*mas 5

我建议,假设它是td你想要选择的第二个元素(而不是任何 td包含该数字的元素2):

tr.Foobar td:nth-of-type(2) {
    /* your CSS */
}
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

tr.Foobar td:nth-child(2) {
    /* your CSS */
}
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

如果您需要支持旧浏览器,不支持:nth-of-type():nth-child(),则可以使用:

tr.Foobar td:first-child + td {
    /* your CSS */
}
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

参考文献: