简体   繁体   English

使用CSS悬停时突出显示表格行

[英]Highlighting a table row when using css hover

I'll try to be as specific as possible. 我会尝试尽可能具体。 I have a table that highlights rows perfectly when the mouse hovers over them using all CSS. 我有一张桌子,当鼠标使用所有CSS悬停在行上时,它们可以完美地突出显示行。 I'd like the user to be able to then click on a row and keep it highlighted until another row is clicked. 我希望用户能够然后单击一行并将其突出显示,直到单击另一行。 Below is some sample code and CSS I'm using to do the highlighting. 以下是一些示例代码和我正在用来突出显示的CSS。 For reference, this is an MVC application which explains the... 作为参考,这是一个MVC应用程序,它解释了...

@foreach (var item in Model) { }

...at the beginning ...一开始

 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> 

Create a class called highlight (or something similar) in your CSS and then attach that class to your row when it's clicked. 在CSS中创建一个称为highlight (或类似名称)的类,然后在单击该类时将该类附加到您的行中。 So, what it should do is remove the highlight class from all table rows, and then add it in to the one that needs highlighted like this. 因此,应该做的是从所有表行中删除突出显示类,然后将其添加到需要突出显示的类中。 Pass in this to the onclick function like so: 像这样将其传递给onclick函数:

onclick="HilightRowOnClick(this)"

and then use the function below. 然后使用下面的功能。 I used your function name: 我用了你的函数名:

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

And then the CSS for the highlight class: 然后CSS的highlight类:

.DBTable tr.highlight {
    background: burlywood;
}

  $(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> 

 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> 

Well the trick is using rgb backGround, the code is incomplete .. I just want show you an alternative way to do that you want it. 好吧,诀窍是使用rgb backGround, 代码不完整 ..我只想向您展示一种您想要的替代方法。 Even though when you use 'this' to select the element... the condition isn't recognizing hexadecimals. 即使使用“ this”选择元素时,条件也无法识别十六进制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM