Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要选择其中值为 2 的 td 元素。我怎么能用纯 CSS 做到这一点?
<tr class="Foobar"> <td>1</td> <td>2</td> <td>3</td> </tr>
谢谢!
我建议,假设它是td您要选择的第二个元素(而不是任何 td包含 number 的元素2):
td
2
tr.Foobar td:nth-of-type(2) { /* your CSS */ }
JS 小提琴演示。
tr.Foobar td:nth-child(2) { /* your CSS */ }
如果您需要支持较旧的浏览器,而不支持:nth-of-type()or :nth-child(),那么您可以使用:
:nth-of-type()
:nth-child()
tr.Foobar td:first-child + td { /* your CSS */ }
参考: