我会尽量具体。我有一个表格,当鼠标悬停在行上时,它可以使用所有 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>
最佳答案
$(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>
关于javascript - 使用 css悬停时突出显示表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48833991/