Javascript jQuery数据表在行上迭代并更改单元格背景

Javascript jQuery数据表在行上迭代并更改单元格背景,javascript,jquery,datatables,Javascript,Jquery,Datatables,我正在尝试迭代DataTable中列(列索引1)中的所有单元格,并使用以下代码根据单元格值更改背景颜色: var table = $('#my_table').DataTable( {...}); console.log("next, iterate over rows in table: "+table); table.rows().every( function ( rowIdx, tableLoop, rowLoop ) { console.log("looping over r

我正在尝试迭代DataTable中列(列索引1)中的所有单元格,并使用以下代码根据单元格值更改背景颜色:

var table = $('#my_table').DataTable( {...});
console.log("next, iterate over rows in table: "+table);
table.rows().every( function ( rowIdx, tableLoop, rowLoop ) { 
    console.log("looping over rows");
    var cell = table.cell({ row: rowIdx, column: 1 }).node();
    if (cell.data() == 'mouse'){
        $(cell).css('background-color', 'orange');
    }
});
console.log("finished iterating over rows ");
该表显示的数据很好。 但是,控制台日志会打印:

>>next, iterate over rows, table: [object Object]
>>finished iterating over rows

table.rows().every( function (...){...} 
未输入。我复制并粘贴了DataTables示例,不知道为什么不执行它

我能想到的唯一一件事是DataTables文档在这里:提及-迭代每个选中的行。表中没有一行是选中的,我只想循环遍历每一行(并更改单元格颜色),如果它被选中或未选中

注:我还尝试:

table.rows().eq(0).each( function ( index ) {
    var row = table.row( index );
    var data = row.data();
    console.log(data)
});

这也不会执行(console.log不会从函数内部打印任何内容)。

您可以迭代表的td元素并捕获每个单元格的文本内容

对这些应用一些逻辑,您可以轻松地指定每个单元格的背景颜色

我使用了arr.map()作为ES6 JavaScript,但是.each()也可以工作

$('table td').map(function(i, cell) {
    var cellContent = $(cell).text();
    console.log(i,cellContent);  // for demonstration
    if (cellContent === 'pending') $(cell).css('background-color', '#ccc');
});

这可以很容易地更改,以获取单元格元素的数据、类或id。

我在页面上有几个表,所以这不起作用。我试着只选择我的表,但我也不知道如何选择td,例如$(“#my_table td”),但这不起作用。我必须在DataTable声明中的“fnintComplete”:function(oSettings,json){}块中添加代码。