【问题标题】:add different background color between odd and even when creating tr in each [duplicate]在每个 [重复] 创建 tr 时,在奇数和偶数之间添加不同的背景颜色
【发布时间】:2017-07-15 13:29:51
【问题描述】:
 var rowCount = 0;
 var trCount;

  // Set start each at second tr
  $('#list tr').each(function(index, tr) {
    // Count
    rowCount++;

    // Create tr
    trCount = $('<tr/>');

    // Create row number 1,2,3
    trCount.append("<td>" + rowCount + "</td>");

    // Append tr to display table
    tableContent.append(trCount);
  });

我正在使用 tr 为我的表创建 tr,但我的意图是奇数和偶数 tr 编号之间会有不同的背景颜色,那么我该怎么做呢?

类似的东西

$("tr:odd").css("background-color", "#E5E5E5");
$("tr:even:gt(0)").css("background-color", "#c7d4e5");

创建&lt;tr&gt;时如何添加不同的背景颜色。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您可以在纯 CSS 中执行此操作,并且无论您使用多少 javascript,它都会应用。

    tr:nth-of-type(even) {
        background-color:#E5E5E5;
    }
    tr:nth-of-type(odd) {
        background-color:#c7d4e5;
    }
    

    【讨论】:

    猜你喜欢
    • 2018-12-27
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2013-08-21
    • 2021-08-26
    • 2014-10-20
    相关资源
    最近更新 更多