将 CSS 应用到表格的第一个和最后一个 TH

Rav*_*avi 0 css jquery css-selectors

我需要使用 jQuery 为下表的第一个和最后一个应用样式,编写了一些未触发的脚本。

 <script>$("th:gt(0)").css("border-left", "none");</script>

 <table>
 <tr>
  <th scope="col">one</th>
  <th scope="col">two</th>
  <th scope="col">three</th>
</tr>
<tr>
  <td>1</td>
  <td>2</td>
  <td>3</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很方便。

Boj*_*les 5

您可以使用:first-child:last-child伪选择器:

$("table tr th:first-child, table tr th:last-child").css("border-left", "none");
Run Code Online (Sandbox Code Playgroud)

要仅将 CSS 应用于最后一个 TH,您可以使用.last(),如下所示(保持一行):

$("table tr th:first-child, table tr th:last-child").css("border-left", "none").last().css("border-right", "none");
Run Code Online (Sandbox Code Playgroud)