【问题标题】:coloring first row in multi tables with jquery用jquery为多表中的第一行着色
【发布时间】:2014-02-15 09:03:18
【问题描述】:

我正在使用这个 css 代码:

.even{
    background: #E4ECF6;
}
.odd{
    background: #F3F7FB;
}
.firstrow{
    background-color: #599ECF;
}

我正在使用这个 css jquery 代码:

$(document).ready(function(){
$('table tr:even').addClass('even');
$('table tr:odd').addClass('odd');
$('table tr:first').addClass('firstrow');
});

此代码在第一个表中正确运行。但是当我有两个或更多表时,此代码可以为第一个表的第一行着色。为所有表中的所有第一行着色更好的代码?

【问题讨论】:

标签: jquery row html-table


【解决方案1】:

尝试替换

$('table tr:first').addClass('firstrow');

$('table tr:first-child').addClass('firstrow');

关键是使用:first-child选择器,

选择作为其父元素的第一个子元素的所有元素。

现场演示here


如果你仍然想要 :first 选择器,你可以这样做:

$(document).ready(function () {
  $('table tr:even').addClass('even');
  $('table tr:odd').addClass('odd');
  $('table').each(function () {  // For each 'table'...
    $(this).find('tr:first').addClass('firstrow');  // Add class 'firstrow' to the first 'tr'.
  });
});

现场演示here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 2010-11-02
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多