如何仅基于标题行应用一些jquery内容 [英] How can I apply some jquery stuff only based on header row

查看:104
本文介绍了如何仅基于标题行应用一些jquery内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的表格:

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



我要添加的类= 红色只向那些 TD 其标题为结果



所以只有在页面加载时动态地使用jquery的结果列。

解决方案

你可以使用



 变种resultHeaderIndex = $( '日:含有( 结果)')
.addClass( '红')
的.index();
$('td:nth-​​child('+(resultHeaderIndex + 1)+')')。addClass('red')


I have the table like this:

<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>

I want to add the class = "red" only to those td whose header is result

so only result column with jquery dynamically when page loads.

解决方案

You can get the index of the header using .index() then apply the class using the :nth-child selector.

jsFiddle

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


If you wanted to add the class to the header also then you can simply add it before you get the index:

jsFiddle

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

这篇关于如何仅基于标题行应用一些jquery内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆