jquery - 悬停时更改表格行背景颜色

标签 jquery html css

我想在悬停在表格行上时实现某种半透明颜色覆盖,我创建了一个小 DEMO在这里。

但是我不想以任何方式改变文本,所以只是背景,我希望它是一种覆盖的原因是我只希望单元格稍微暗一些而不是相同的悬停时的颜色。有道理吗?

代码:

<table id="compTable" class="prTable">
    <tr class="prTableHeader">
        <th></th>
        <th></th>
        <th>1</th>
        <th>2</th>
        <th>3</th>
    </tr>
    <tr class="prTableRow">
        <td class="prExerVariNameTD blackColor">Squat</td>
        <td class="prVerticalSpace"></td>
        <td class="prTableCell" title="@finalDate">100</td>
        <td class="prTableCell" title="@finalDate">100</td>
        <td class="prTableCell" title="@finalDate">100</td>
    </tr>
</table>

CSS

.prTableCell {
    padding: 7px;
    width: 60px;
    text-align:center;
    border:1px solid #e1e1e1;
    cursor:default;
}

.prExerVariNameTD {
    width: 200px;
    border:1px solid #e1e1e1!important;
    padding: 5px 5px 5px 10px;
    background-color: white;
}

.prVerticalSpace {
    width: 50px;
}

.rowHover {
    /*background: rgba(204, 204, 204, 0.5);*/
    opacity: 0.5;
}

脚本:

$(document).ready(function () {
    $("table#compTable .prTableCell:even").css("background-color", "#eeeeee");
    $("table#compTable .prTableCell:odd").css("background-color", "White");

    $(".prTableRow").hover(function () {
        $(this).toggleClass("rowHover");
    });
});

最佳答案

不需要 JavaScript。 CSS 来拯救!

这会在悬停时将背景颜色设置得稍微暗一些。您可以使用实际颜色值来使鼠标悬停时颜色变深或变亮。

.prTableCell {
  padding: 7px;
  width: 60px;
  text-align: center;
  border: 1px solid #e1e1e1;
  cursor: default;
}

.prExerVariNameTD {
  width: 200px;
  border: 1px solid #e1e1e1!important;
  padding: 5px 5px 5px 10px;
  background-color: white;
}

.prVerticalSpace {
  width: 50px;
}

.prTableCell:nth-child(even) {
  background-color: #eee;
}

.prTableCell:nth-child(odd) {
  background-color: #fff;
}

.prTableRow:hover .prTableCell:nth-child(even) {
  background-color: #ddd;
}

.prTableRow:hover .prTableCell:nth-child(odd) {
  background-color: #eee;
}
<table id="compTable" class="prTable">
  <tr class="prTableHeader">
    <th></th>
    <th></th>
    <th>1</th>
    <th>2</th>
    <th>3</th>
  </tr>
  <tr class="prTableRow">
    <td class="prExerVariNameTD blackColor">Squat</td>
    <td class="prVerticalSpace"></td>
    <td class="prTableCell" title="@finalDate">100</td>
    <td class="prTableCell" title="@finalDate">100</td>
    <td class="prTableCell" title="@finalDate">100</td>
  </tr>
</table>

关于jquery - 悬停时更改表格行背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43530464/

相关文章:

javascript - 在滚动 div 颜色变化

javascript - 想要在不更改类名的情况下向类添加定义元素

html - 最大高度和最大宽度

asp.net - 为什么我需要使用 .d 来访问 jQuery AJAX 返回的数据?

javascript - 幻灯片放映第一张幻灯片超时时间较长

html - 如何使元素与溢出保持在同一行

html - 左对齐表格不适用于 CSS,但适用于 HTML

javascript - 如何获取动态输入框生成的两个文本框的倍数

javascript - 动态获取 vimeo 视频的缩略图和标题

jquery - jquery startwith 选择器问题