【问题标题】:Hover td escapes background-color change from hover tr悬停 td 从悬停 tr 转义背景颜色更改
【发布时间】:2014-02-27 00:18:49
【问题描述】:

我有一个用于表格行的通用类,当它们悬停时,它会更改它的 td 的 background-color。我在整个应用程序中都使用它。

.enty-table {
    &:hover > td {
        background-color: lightblue;
    }
}

 <table class="table table-bordered">
     <thead>
         <tr>
             <th></th>
             <th>Name</th>
             <th>Min Length</th>
             <th>Max Length</th>
             <th>Min Value</th>
             <th>Max Value</th>
             <th>Regular Expr</th>
             <th>Default Value</th>
         </tr>
     </thead>
     <tbody>
         <tr class="enty-table">
             <!-- I want the hover on the folowing td to escape the hover effect added with enty-table class on tr -->
             <td class="columnCategory">Business Fields</td>
             <td><strong>new column1</strong></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
          </tr>
          <tr class="enty-table">
             <td><strong>new column2</strong></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
             <td><span></span></td>
          </tr>
      </tbody>
 </table>

现在,我有一个特殊的表(二维),在第一列中,我有 td 和 rowspan。

当我将鼠标悬停在 rowspan td 时,最终我想摆脱背景颜色的变化:

当我将鼠标悬停在Business Fields td 上时,悬停效果应用于new column1 行,但是当我悬停在第二行时,它不会应用于 td 与 rowspan。我想通过从第一个 td 中删除悬停动作来解决这个问题。

如何避免 rowspan td 上的悬停效果,但保留表格行(单个子行 - new column1new column2)?

只能通过 CSS 完成吗?

【问题讨论】:

  • 向我们展示您的 html.. 或者更好地提供 jsfiddle.net

标签: javascript jquery html css hover


【解决方案1】:

您可以使用 CSS :not() 伪类忽略 &lt;td&gt; 具有 rowspan 属性,然后使用 CSS general sibling selectors 重置表格单元格的背景颜色,如下所示:

.enty-table {
  &:hover > td:not([rowspan]) {
    background-color: lightblue;
  }

  & > td[rowspan]:hover ~ td {
    background-color: #fff; /* Reset the background of next cells */
  }
}

JSBin Demo.

更新

如果不能使用 CSS :not(),您可以重置第一个单元格的 background-color,如下所示:

.enty-table {
  &:hover > td {
    background-color: lightblue;
    /* Reset the background color of td[rowspan] */
    &[rowspan] {
      background-color: #fff;
    }
  }

  & > td[rowspan]:hover ~ td {
    background-color: #fff; /* Reset the background */
  }
}

JSBin Demo #2.


基本上我需要的是,当我将鼠标悬停在 td 上时,不会在 tr 上应用任何内容

实际上,您正在悬停tr 本身,而不仅仅是td(指td[rowspan]

是否有可能在 CSS 的树结构中走得更高

CSS 是级联的,没有后向和/或父选择器 (yet)。

作为纯 CSS 方式,您可以在 td[rowspan] 上使用 pointer-events: none; 来防止触发该元素上的鼠标事件。

Working Demo #3

否则,除了td[rowspan] 之外,您需要使用 JavaScript 更改所有悬停的表格单元格。

例如:

$('.enty-table').children('td:not(td[rowspan])').hover(function() {
  $(this).siblings(':not(td[rowspan])').addBack().addClass('hover');
}, function() {
  $(this).parent().children('td').removeClass('hover');
});

Working Demo #4.

【讨论】:

  • 这几乎对我有用!您的示例重写了邻居 td 的适当性。就我而言,我想避免它。基本上我需要的是,当我将鼠标悬停在 td 上时,不会在 tr 上应用任何内容。我不是 CSS 开发人员,所以我不知道:是否有可能在 CSS 的树结构中走得更高,或者解决方案是 JavaScript?
  • @razvan 我刚刚更新了答案以满足您的需求。
  • 是的。对所有方法都有很好的解释!谢谢。
【解决方案2】:

你可以做几件事:

.enty-table {
    & td:hover {
        background-color: lightgrey;
    }
}
.enty-table {
    & tr:hover {
        background-color: lightgrey;
    }
}

【讨论】:

  • 您可以在第一个选择器处删除&amp; 。在 & 号后面使用空格字符在这里没有意义。
【解决方案3】:

请试一试。使用类树并使用选定对象更改样式。它适用于所有使用正确树模式的“TD”。

$(document).ready(function(){
  $(".enty-table tr td").hover(function(){
    $(this).css("background-color","yellow");
  });
  $(".enty-table tr td").mouseout(function(){
    $(this).css("background-color","lightgray");
  });
});

http://jsfiddle.net/vDD5p/

【讨论】:

  • 我不想在单个 td 上应用悬停。我想悬停在 td 元素上以逃避父父 tr 的悬停。 tr 的其余部分应该具有正常的悬停效果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多