CSS 如何仅选择第一个和最后一个 <th>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4498585/
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 23:21:31  来源:igfitidea点击:

How to select first and last <th> only?

csshtml-table

提问by sami

My table has id="mytable". I'm trying to apply some CSS on the first and last <th>only. I tried this but it doesn't work.

我的桌子上有id="mytable"。我正在尝试仅在第一个和最后一个应用一些 CSS <th>。我试过这个,但它不起作用。

#mytable th:first,
#mytable th:last{
 //css goes here
}

回答by Codler

#mytable th:last-child, #mytable th:first-child {
 //css goes here
}

回答by nikhil

For CSS

对于 CSS

#myTable thead tr th:first,
#myTable thead tr th:last{
  /*CSS goes here*/
}

Using Jquery (for last child only):

使用 Jquery(仅适用于最后一个孩子):

$("#mytable th:last-child").css({

/*CSS goes here*/

});