【问题标题】:selected row color for striped table条纹表的选定行颜色
【发布时间】:2019-10-06 15:35:31
【问题描述】:

我希望表格的选定行具有某种背景颜色。 我的桌子是条纹的,它只适用于白色行。我正在使用 datatables.net。

如果选择灰色行,那么我希望灰色行的颜色相同,然后选择白色行。但是使用此代码,它会通过单击将白色行变为灰色。但是,如果我只使用第一个 if else 块,它适用于白色行。我做错了什么?

if (this.style.background == 'white') {
  $(this).css('background', '#cce6ff');
} else {
  $(this).css('background', 'white');
}

if (this.style.background == 'lightgrey' && this.style.background != 'white') {
  $(this).css('background', '#cce6ff');
} else if (this.style.background != 'white') {
  $(this).css('background', 'lightgrey');
}

这也不起作用:

  if (this.style.background == 'white'){
        $(this).css('background', '#b6c7db');
        break;
    }else{
        $(this).css('background', 'white');
        break;
    }
    if (this.style.background == 'lightgrey' && this.style.background != 'white'){
        $(this).css('background', '#cce6ff');

    }else if (this.style.background != 'white'){
        $(this).css('background', 'lightgrey');}

【问题讨论】:

  • 如果我使用 #7F7F7F 这样的#颜色,它根本不起作用......
  • 为什么不在 CSS 中这样做并省去这个头疼的问题
  • 希望拼写是正确的——浅灰色而不是浅灰色
  • 它在 css 中不起作用。我试过了:tr.selected { background: #cce6ff !important}
  • 您首先询问它是否为白色,如果是,则将其设置为颜色#cce6ff。然后在 if else 你问的不是白色然后将其设置为灰色。这就是他将其设置为灰色的原因。

标签: javascript jquery css model-view-controller datatable


【解决方案1】:

您需要确保this 实际上是指该行。如果没有,您将需要调整它以定位该行。

也就是说,假设数据表已经将选定的类添加到行中,您可以使用 CSS 来定位它。

然后,如果上述方法仍然不起作用,则很有可能您需要使用 !important 强制该值,这无疑会在其他地方被覆盖

方法二: 您应该使用您拥有的 CSS 规则来执行此操作,并避免检查当前颜色。在选择时,从所有行中删除 selected 类并在被选择的行上切换它。

您的代码是否在点击处理程序中?您的第二个 sn-p 中有 break 语句,此代码是否在循环中?我觉得你对条件语句有一些误解,但我很难理解那些代码 sn-ps 试图实现的目标——例如,this.style.background == 'lightgrey' && this.style.background != 'white' 是不必要的冗长,如果背景等于lightgrey,那么它不会t等于white,可以去掉第二个比较。

【讨论】:

  • table.dataTable tbody tr.selected { background-color: pink !important; } 这样的事情不起作用......不幸的是
  • 你能给我们看一些 HTML 吗?也许把你的工作放到 JSFiddle @ZaraZahara
  • 您需要将其应用于 TD/TH 即 table.dataTable tbody tr.selected td, table.dataTable tbody tr.selected th { background-color: white; } @ZaraZahara
  • @ZaraZahara 有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 2020-03-14
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多