我有这样的表:

<table>
<thead>
   <th>id </th><th>name </th><th>number </th><th>result </th>
</thead>
<tbody>
<tr>
 <td>stuff</td>
 <td>stuff</td>
 <td>stuff</td>
 <td>stuff</td>
</tr>
</tbody>
 </table>


我只想将class = "red"添加到标题为tdresult

因此,仅在页面加载时动态地使用jquery生成结果列。

最佳答案

您可以使用.index()获取标头的索引,然后使用:nth-child selector应用该类。

jsFiddle



var resultHeaderIndex = $('th:contains("result")').index();
$('td:nth-child(' + (resultHeaderIndex + 1) + ')').addClass('red')




如果您还想将类添加到标头中,则只需在获取索引之前将其添加即可:

jsFiddle



var resultHeaderIndex = $('th:contains("result")')
    .addClass('red')
    .index();
$('td:nth-child(' + (resultHeaderIndex + 1) + ')').addClass('red')

关于javascript - 我如何仅基于标题行应用一些jQuery的东西,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15651408/

10-12 00:21
查看更多