2017-07-11 18 views
1

我正在使用dataTables,並且當滿足特定要求時我想將th的字體顏色更改爲紅色,但它不起作用。 這個th顯示一列的總和。數據通過DataTables api動態插入。用jquery在DataTable中更改<th>的css值

如果我更改要執行.css(「color」,「red」)的元素,只要它位於表格外部,它就會工作。

jQuery的

desna = $('#skupina').DataTable({ 
    "paging": false, 
    "scrollY": "700px", 
    "scrollCollapse": true, 
    //stateSave: true, 
    bInfo: false, 
    "columnDefs": [ 
     { 
      "targets": [ 1 ], 
      "visible": false, 
      "searchable": false 
     }],  
    "footerCallback": function (row, data, start, end, display) { 
     var api = this.api(), data; 

     // Remove the formatting to get integer data for summation 
     var intVal = function (i) { 
      return typeof i === 'string' ? 
       i.replace(/[\$,]/g, '') * 1 : typeof i === 'number' ? i : 0; 
     }; 

     // Total over all pages 
     total = api 
      .column(3) 
      .data() 
      .reduce(function (a, b) { 
       return intVal(a) + intVal(b); 
      }, 0); 

     // Total over this page 
     pageTotal = api 
      .column(3, { page: 'current'}) 
      .data() 
      .reduce(function (a, b) { 
       return intVal(a) + intVal(b); 
      }, 0); 

     // Update footer 
     $(api.column(3).footer()).html(
      pageTotal +' kg' 
     ); 
    } 
}); 
$('#druga').on("change", "#vozilo", function() { 
    var g = $('#grupa').val(); 
    max_kg = parseInt($(this).children(":selected").attr("id")); 
    if(max_kg<pageTotal){ 
     alert("nope"); 
     $("#foto").css("color", "red" ); 
    }; 
}); 

PHP

function zaglavlje($row, $rbr) { 

echo '<thead>'; 
echo '<tr> 
    <th>Adresa</th> 
    <th>Grad</th> 
    <th>Drzava</th> 
    <th><input type="checkbox" id="all'.$rbr.'" align="center"/></th> 

</tr> 
<tfoot> 
     <tr> 
      <th colspan="10" id="foto" contenteditable="true"></th> 
     </tr> 
    </tfoot>'; 
echo '</thead>'; 

} 
+1

沒有代碼,我們不能幫你設置列... – errand

+1

把你的代碼這裏。 –

+0

添加運行示例代碼 –

回答

0

我給這屆的一類,它的工作,當我打電話的.css()這個類的。

+0

很難相信'css()'在現有的類上工作,但不是現有的ID。其實這是完全不可能的:) – davidkonrad

0

我認爲你正在尋找

$('#example').dataTable({ 
    "drawCallback": function(settings) { 
     $('#example tr:eq(0) th:eq(0)').text("Text update by code"); 
    } 
    }); 

一樣的,如果你想使用數據表,然後請click Here