javascript - 使用 css悬停时突出显示表格行

标签 javascript jquery html css asp.net-mvc

我会尽量具体。我有一个表格,当鼠标悬停在行上时,它可以使用所有 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/

相关文章:

javascript - AnimTrap CSS动画循环

html - 在不更改背景图像不透明度的情况下更改背景颜色不透明度

javascript - 为什么这个 html5 音频在 chrome 上不起作用,但在 safari 上运行良好

jquery - AJax 不适用于 bootstrap-select

javascript - 组织大型 JavaScript 应用程序的工具和最佳实践

javascript - IE8 的 jquery.corner 问题

javascript - 如何更改禁用控件的颜色

html - 容器 div 内的多个 div

javascript自动刷新div而不刷新整个页面

javascript - 使用 React Immutability helpers 或 Immutable js React 合并不可变对象(immutable对象)数组