简体   繁体   English

使用jQuery在html表中为列添加类

[英]Add class for the column in the html table using jQuery

Please tell me how to add my class for the column in the table (all the cells of the selected column) using jQuery? 请告诉我如何使用jQuery为表中的列(所选列的所有单元格)添加类?

For example, I need a column №2 例如,我需要一列№2

<table id = 'mytable'>
<tbody>
<tr><td></td>...<td></td></tr>
...
<tr><td></td>...<td></td></tr>
</tbody>
</table>

$('#mytable td.eq( 1 )').addClass('myclass'); // set class for only one cell :(

I want 我想要

<table id = 'mytable'>
<tbody>
<tr><td></td><td class = 'myclass'></td>...<td></td></tr>
...
<tr><td></td><td class = 'myclass'></td>...<td></td></tr>
</tbody>
</table>

You can use CSS :nth-child() selector to add rules, no need use 您可以使用CSS :nth-child()选择器添加规则,无需使用

 #mytable td:nth-child(2) { color: red; } 
 <table id='mytable'> <tbody> <tr> <td>1</td> <td>2</td> </tr> <tr> <td>3</td> <td>4</td> </tr> </tbody> </table> 

You can use :nth-child() selector: 您可以使用:nth-​​child()选择器:

 $('#mytable tr td:nth-child(2)').addClass('myClass');

The nth-child will allow you to select the desired column by specifying the column number. nth-child将允许您通过指定列号来选择所需的列。 Also, you need to include JQuery plugin in your HTML file. 另外,您需要在HTML文件中包含JQuery插件。

  $('#mytable tr td:nth-child(2)').addClass('myClass'); 
 td:nth-child(2) { color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="mytable"> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> 

You could do with each() function. 您可以使用each()函数。 And change the :eq(1) instead of .eq(1) .its a wrong jquery object 并更改:eq(1)而不是.eq(1) 。它是一个错误的jquery对象

$('#mytable tbody tr').each(function(){
$(this).find('td:eq(1)').addClass('myclass');
})

 $('#mytable td:nth-of-type(2)').addClass('myclass'); 
 .myclass { color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id='mytable'> <tbody> <tr> <td>first</td> <td>second</td> <td>thirth</td> </tr> <tr> <td>first</td> <td>second</td> <td>thirth</td> </tr> </tbody> </table> 

You can also do like this using Jquery... It will work for your code... which selects element td which comes after another td... 您也可以使用Jquery这样操作...它将对您的代码有效...选择元素td紧随另一个td之后...

$('#mytable tr td~td').addClass('myClass');

for specific selection... use nth child... 对于特定选择...使用第n个孩子...

$('#mytable tr td:nth-child(2)').addClass('myClass');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM