【问题标题】:Highlighting a table row when using css hover使用css悬停时突出显示表格行
【发布时间】:2018-02-16 19:57:55
【问题描述】:

我会尽量做到具体。当鼠标悬停在使用所有 CSS 的行上时,我有一个表格可以完美地突出显示行。我希望用户能够然后单击一行并使其突出显示,直到单击另一行。下面是一些示例代码和我用来突出显示的 CSS。作为参考,这是一个 MVC 应用程序,它解释了...

@foreach (var item in Model) { }

...开头

function HilightRowOnClick() {
    //alert($(row).closest('tr').index())

    $(document).ready(function () {
        $('tr').click(function () {
            //if (this.style.background == "" || this.style.background == "#badecc") {
            if (this.style.backgroundColor == "#badecc") {
                alert($("Color is normal?"));
                $(this).css('background', 'burlywood');
            }
            else {
                $(this).css('background', '#badecc');
                alert("Color is not normal?");
            }
        });
    });
}
.DBTable {
    width: 100%;
}


.DBToprow {
    font-size: 180%;
    font-weight: 600;
}


.DBTable td {
    font-size: 50%;
    padding: 7px;
}


.DBTable th {
    border-style: solid;
    border-width: 1px;
    padding: 7px;
}


.DBTable tr {
    background: #badecc;
    border-style: solid;
    border-width: 1px;
}

.DBTable tr:hover {
    background-color: burlywood;
}
<table class="DBTable">
    <tr class="DBToprow">
        <td></td>
        <td>
            @Html.DisplayNameFor(model => model.ClientID)
        </td>
        <td>
            @Html.DisplayNameFor(model => model.Active)
        </td>
        <td>
            @Html.DisplayNameFor(model => model.BankID)
        </td>
    </tr>

    @foreach (var item in Model)
    {
        <tr onclick="HilightRowOnClick()">
            <th>
                @Html.DisplayFor(modelItem => item.ClientID)
            </th>
            <th>
                @Html.DisplayFor(modelItem => item.Active)
            </th>
            <th>
                @Html.DisplayFor(modelItem => item.BankID)
            </th>
        </tr>
    }
</table>

【问题讨论】:

    标签: javascript jquery html css asp.net-mvc


    【解决方案1】:

        $(document).ready(function () {
            $('tr').click(function () {
                $('tr').removeClass("clicked");
                $(this).addClass("clicked");
            });
        });
    .DBTable {
        width: 100%;
    }
    
    
    .DBToprow {
        font-size: 180%;
        font-weight: 600;
    }
    
    
    .DBTable td {
        font-size: 50%;
        padding: 7px;
    }
    
    
    .DBTable th {
        border-style: solid;
        border-width: 1px;
        padding: 7px;
    }
    
    
    .DBTable tr {
        background: #badecc;
        border-style: solid;
        border-width: 1px;
    }
    
    .DBTable tr.clicked {
        background: burlywood;
        border-style: solid;
        border-width: 1px;
    }
    
    
    
    .DBTable tr:hover {
        background-color: burlywood;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table class="DBTable">
        <tr class="DBToprow">
            <td></td>
            <td>
                @Html.DisplayNameFor(model => model.ClientID)
            </td>
            <td>
                @Html.DisplayNameFor(model => model.Active)
            </td>
            <td>
                @Html.DisplayNameFor(model => model.BankID)
            </td>
        </tr>
    
        @foreach (var item in Model)
        {
            <tr>
                <th>
                    @Html.DisplayFor(modelItem => item.ClientID)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Active)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.BankID)
                </th>
            </tr>
        }
    </table>

    【讨论】:

      【解决方案2】:

      function HilightRowOnClick() {
          //alert($(row).closest('tr').index())
      
          $(document).ready(function () {
              $("tr").click(function () {
                    console.log(this.style.backgroundColor);
                  //if (this.style.background == "" || this.style.background == "#badecc") {
                  if (this.style.backgroundColor == 'rgb(186, 222, 204)') {
                      console.log('#1');
                      this.style.backgroundColor = 'red';
                  }
                  else {
                      console.log('#2');
                      this.style.backgroundColor = '#badecc';
                      
                  }
              });
          });
      }
      .DBTable {
          width: 100%;
      }
      
      
      .DBToprow {
          font-size: 180%;
          font-weight: 600;
      }
      
      
      .DBTable td {
          font-size: 50%;
          padding: 7px;
      }
      
      
      .DBTable th {
          border-style: solid;
          border-width: 1px;
          padding: 7px;
      }
      
      
      .DBTable tr {
          background: #badecc;
          border-style: solid;
          border-width: 1px;
      }
      
      .DBTable tr:hover {
          background-color: burlywood;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <table class="DBTable">
          <tr class="DBToprow">
              <td></td>
              <td>
                  @Html.DisplayNameFor(model => model.ClientID)
              </td>
              <td>
                  @Html.DisplayNameFor(model => model.Active)
              </td>
              <td>
                  @Html.DisplayNameFor(model => model.BankID)
              </td>
          </tr>
      
          @foreach (var item in Model)
          {
              <tr onclick="HilightRowOnClick()">
                  <th>
                      @Html.DisplayFor(modelItem => item.ClientID)
                  </th>
                  <th>
                      @Html.DisplayFor(modelItem => item.Active)
                  </th>
                  <th>
                      @Html.DisplayFor(modelItem => item.BankID)
                  </th>
              </tr>
          }
      </table>

      诀窍是使用 rgb 背景,代码不完整.. 我只是想向您展示另一种方法来做到这一点。即使当您使用 'this' 选择元素时...条件无法识别十六进制。

      【讨论】:

      • 愚蠢的,从来没有考虑过十六进制是一个可能的问题。
      【解决方案3】:

      在您的 CSS 中创建一个名为 highlight(或类似名称)的类,然后在单击该类时将该类附加到您的行。因此,它应该做的是从所有表行中删除突出显示类,然后将其添加到需要像这样突出显示的行中。将其传递给onclick 函数,如下所示:

      onclick="HilightRowOnClick(this)"
      

      然后使用下面的函数。我用了你的函数名:

      function HilightRowOnClick(el) {
          $('tr').removeClass('highlight');
          $(el).addClass('highlight');
      }
      

      然后是 highlight 类的 CSS:

      .DBTable tr.highlight {
          background: burlywood;
      }
      

      【讨论】:

      • 仍然没有变化,我搞砸了可能我输入错误样式标签的内容
      • 好吧,无论如何我都会为您设置一个代码笔以供参考。
      • 谢谢,我会搞砸的,看看我做错了什么
      猜你喜欢
      • 2012-02-04
      • 2018-01-07
      • 2019-02-17
      • 2016-03-29
      • 2023-03-18
      • 2020-03-29
      • 1970-01-01
      • 2013-11-26
      • 2012-05-13
      相关资源
      最近更新 更多