Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改jQuery jTable特定行中的背景色?_Jquery_Css_Jquery Jtable - Fatal编程技术网

如何更改jQuery jTable特定行中的背景色?

如何更改jQuery jTable特定行中的背景色?,jquery,css,jquery-jtable,Jquery,Css,Jquery Jtable,上面的代码可能正在更改所有行的颜色,我可以指定行号吗?下面的示例将更改第4行的背景颜色 rowInserted: function (event, data) { if (data.record) { if (condition1 == condition2) { $('#div1').find(".jtable tbody tr").css("background", "#F5ECCE"); } } } 或者,您可以使用伪类选择器:

上面的代码可能正在更改所有行的颜色,我可以指定行号吗?

下面的示例将更改第4行的背景颜色

rowInserted: function (event, data) {
   if (data.record) {
       if (condition1 == condition2) {
          $('#div1').find(".jtable tbody tr").css("background", "#F5ECCE");
       }
   }
}
或者,您可以使用伪类选择器:eq来选择只需要的数字,这也使用基于0的索引

var $rows = $('#div1').find(".jtable tbody tr");
var ROWNUMBER = 3;
$($rows[ROWNUMBER]).css("background", "#F5ECCE");
使用like

更新您可以像这样动态设置索引

试试这个

$('#div1').find(".jtable tbody tr:eq("+index+")").css("background", "#F5ECCE");
动态获取行id的步骤 像下面这样使用

基于Raeviman的答案,由于一点细节,该答案对我不起作用: 使用rowIndex,我得到的索引从1开始,但是下面的CSS路径的第一行索引为0,因此当我将值赋给变量index时为-1


这将正确地以红色突出显示值字段大于100的行。

Rohan Kumar和Raeviman的答案很好。但是使用data.row可以缩短代码并加快运行速度

        rowInserted: function(event, data) {
            if (data.record.value>100){
                var index = data['row'][0]['rowIndex']-1;
                console.log('decorating row with index: '+index);
                $('#div1').find(".jtable tbody tr:eq("+index+")").css({"background":"red"});

            }
        }

要在加载表后执行此操作,并且如果知道行的键,可以使用以下命令:

rowInserted: function (event, data) {
   if (data.record) {
       if (condition1 == condition2) {
          data.row.css("background", "#F5ECCE");
       }
   }
}

是否可以动态获取行?我从何处获取indexrow number值?@user2119324索引和从0开始的行号在此处是相同的。如果要获取当前索引,应写入:var rowCount=$'.jtable tr'.length;然后在您的索引->在eq选择器中写入rowCount-1是否可以动态获取行数?是否可以动态获取行数?@user2119324您可以将行数设置为任意值,或者,如果要在末尾追加行,您可以使用$'div1'。find'.jtable tbody tr'.last.css
var rowNumber = 1;
$('#div1').find(".jtable tbody tr").eq(rowNumber).css("background", "#F5ECCE");
rowInserted : function(event, data) 
        {
           var index = data['row'][0]['rowIndex'];
           $('#npoDiv').find(".jtable tbody tr:eq("+index+")").css("background", "green");
        }
        rowInserted: function(event, data) {
            if (data.record.value>100){
                var index = data['row'][0]['rowIndex']-1;
                console.log('decorating row with index: '+index);
                $('#div1').find(".jtable tbody tr:eq("+index+")").css({"background":"red"});

            }
        }
rowInserted: function (event, data) {
   if (data.record) {
       if (condition1 == condition2) {
          data.row.css("background", "#F5ECCE");
       }
   }
}
    $("tr[data-record-key=" + rowKey + "]").addClass(rowClass);