2015-06-30 57 views
0

我在我的项目中使用jQuery和数据表。添加CSS到jQuery数据表中的特定单元格

我现在想要的是CSS添加到细胞上的按钮,点击即改变细胞color.I只想改变单一的特定单元格的值不是整行

我尝试以下方法,它没有工作

<table id="food"> 
    <thead> 
     <tr> 
      <th>Number</th> 
      <th>Name</th> 
      <th>Type</th>       
     </tr>       
    </thead>  
    <tbody> 
     <tr> 
      <td>1</td> 
      <td>Apple</td> 
      <td>Fruit</td> 
     </tr> 
     <tr> 
      <td>2</td> 
      <td>Mango</td> 
      <td>Fruit</td> 
     </tr> 
     <tr> 
      <td>3</td> 
      <td>Grape</td> 
      <td>Fruit</td> 
     </tr> 
    </tbody> 
</table> 

<button id='button1'>Click me</button> 

这里是jQuery的

$('#button1').click(function(){ 

    //Here I want to change row 1,col 3 to red color 

    var cell = table.row(1).column(3).node(); 
    $(cell).addClass('change-color'); 

}); 

感谢

+0

你想更改CSS整个排?这个复选框在哪里?每行的第一列?请在问题 – Dhiraj

+0

中更多描述你的情况嗨dhiraj, 删除所有不必要的东西..现在问题看起来简单明了.. – TheLion

+0

你可以请提供表格的HTML吗? – Wazaaaap

回答

0

您还可以更改所有值界定具有一列增量I +数字

这项工作与jQuery数据表

var myTable = $('#example2').DataTable(); 
var columnas = new Array(); 
    columnas[4] = 4; 
$.each(columnas, function(i, v) { 
    var x = i+3; 
    var nodes = myTable.column([x]).nodes(); 
    var y = i+3; 
    $.each(nodes, function(y, v) { 
     $(nodes[y]).addClass('change-color');    
    }); 
}); 
+0

https://jsfiddle.net/9r0onvf0/#&togetherjs=UmmARBKedM –

+0

链接地址:上次测试https://jsfiddle.net/9r0onvf0/8/#&togetherjs=UmmARBKedM –

相关问题