更改html表背景颜色,以动态添加偶数行

时间:2017-05-31 09:11:46

标签: html css

我有以下示例html表定义。

   <table id="myDynamicTable"  class="table-striped" >
            <thead class="ui-widget-header_custom dataTables_wrapper no-footer">
            <tr id="uploadrow_0" class="">
                <th style="white-space: nowrap;display: table-cell;width: 2px; text-align: center " class="text-left" >
                    Row#
                </th>
                   <th  style="white-space: nowrap;display: table-cell;text-align: center " class="text-left msr_d_col_widths_nationality">
                    Nationality
                </th>
                <th style="white-space: nowrap;display: table-cell; text-align: center " class="text-left">
                    No of Visitors
                </th>
                <th style="white-space: nowrap;display: table-cell;text-align: center " class="text-left msr_d_col_widths_remark">
                    Remarks
                </th>
            </tr>
        </thead>
        <tbody>
                      @if (Model.VisitDetails.Any())
                      {
                          foreach (var item in Model.VisitDetails)
                          {
                              @Html.Partial("VisitDetailsPartial", item);
                          }
                      }
                      else
                      {
                          item.RowId = 1;
                          item.NationalityList = ViewBag.NationalityList;
                          @Html.Partial("VisitDetailsPartial", item);
                      }
        </tbody>
    </table>

单击按钮,将在asp.net MVC局部视图中定义的行添加到表中。

点击按钮

$(document).ready(function () {
        var tableBody = $('#myDynamicTableDiseases tbody');
        var url = '@Url.Action("Add", "Report")';
        $('.btnAddRow').click(function () {
            $.get(url, function (response) {
                tableBody.append(response);
                $('#myDynamicTableDiseases tbody tr').each(function (idx) {
                    $(this).children("td:eq(0)").html(idx + 1);
                });
            });
        });
    });

“报告”控件中的“添加”操作会将“VisitDetailsPartial”作为添加到表格中的新行返回。

以下是VisitDetailsPartial定义。

 @model SolutionName.ViewModel.VisitDetailsViewModel
 <tr class="">
    @using (Html.BeginCollectionItem("item"))
    {
        <td class="autoGenNumber" style="width: 5px" >
              @if (Model == null)
            {
                var item = new VisitDetailsViewModel
                {
                    NationalityList = ViewBag.NationalityList,
                };
                @Html.LabelFor(x => item.RowId, item.RowId.ToString(), new { style = "", @class = "autoGenNumber" })
            }
            else
            {
                @Html.LabelFor(x => Model.RowId, Model.RowId.ToString(), new { style = "", @class = "autoGenNumber" })
            }  
        </td>
            <td class="">
            @Html.DropDownListFor(model => model.NationalityId, new SelectList(Model.NationalityList, "Value", "Text", Model.NationalityId), "Select", new { @id = "ddlNationalityList" })
        </td>
       <td class="">
            @Html.TextBox("txtNumberOfVisits", Model.NumberOfVisits, new { id = "txtNumberOfVisits"})    
        </td>
         <td class="">
            @Html.TextBoxFor(x => Model.Remarks, new { id = "txtRemarks", Multiline = true})
        </td>
    } 
</tr>

我正在尝试使用下面的CSS来更改动态添加的偶数表行的第一列中的背景颜色,但CSS未应用。

.table-striped > tbody > tr:nth-of-type(even) td:first-child {
  background-color: #e0f0ff;

}

如果我将其应用于其行不是来自局部视图的表,则CSS工作正常。

不确定我上面缺少什么。

1 个答案:

答案 0 :(得分:0)

好的,我通过以下修改CSS来实现它,虽然在上面的标记中,我似乎无法理解为什么我认为第1列实际上被视为第2列。

此版本有效。

    .table-striped > tbody > tr:nth-of-type(even) > td:nth-child(2) {
    background-color: #e0f0ff; 
}
相关问题