【问题标题】:manipulating table row colours when click and hover, conflicting when using zebra row colours单击和悬停时操作表格行颜色,使用斑马行颜色时发生冲突
【发布时间】:2015-05-06 11:17:10
【问题描述】:

我有一个包含许多行的表,使用 jQuery im 捕获单击和悬停,然后使用添加和删除类更改颜色。当您单击该行时,该行突出显示为绿色,当您将鼠标悬停时,该行突出显示为粉红色。

$("#div2 tr").click(function() {
    $('tr').removeClass('highlight2');  
    $(this).addClass('highlight2');
});

$('#div2 tr').hover(function() {
    $(this).addClass('highlight1');
}, function() {
   $(this).removeClass('highlight1');        
});

.highlight1 {
    background-color: #FFD6F5;
}
.highlight2 {
    background-color: #CEF6CE;
}

我还想做的是使用 2 色调灰色的斑马线,但是当我使用额外的 CSS 执行此操作时,它会阻止上述操作,并且由于我缺乏 CSS im,我相信会发生冲突无法解决此问题,因此需要一些帮助或尽可能指出正确的方向。

tr:nth-child(even) {background: #F2F2F2}
tr:nth-child(odd) {background: #FAFAFA}

【问题讨论】:

  • 高亮类背景色css错误?您错过了颜色代码的 #,还是拼写错误?
  • 这是一个仅使用 css 的演示,单击并按住该行以查看单击时的颜色。 jsfiddle.net/abhiklpm/uv7csbec
  • @abhiklpm - 为什么不将其添加为答案?
  • @abhiklpm,当单击我希望更改颜色时,即使您将鼠标移开,直到单击另一行,其原因是,当单击一行时,它会打开该记录向上,这样您就可以随时知道您正在查看的记录。

标签: jquery css


【解决方案1】:

一般定义tr:nth-child(even)的优先级高于highlight1之类的类。但是,您可以通过为选定/单击行的单元格着色来获得技巧。请参阅下面的示例:

$("#div2 tr").click(function() {
    $('tr').removeClass('highlight2');  
    $(this).addClass('highlight2');
});

$('#div2 tr').hover(function() {
    $(this).addClass('highlight1');
  
}, function() {
    $(this).removeClass('highlight1');  
});
table {
  width: 100%;
  border-spacing: 0;
  border-collapse: collapse;
}
td {
  border: solid 1px black;
}
tr:nth-child(even) {
  background-color: #F2F2F2;
}
tr:nth-child(odd) {
  background-color: #FAFAFA;
}
.highlight1 td {
    background-color: #FFD6F5;
}
.highlight2 td {
    background-color: #CEF6CE;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div2'>
  <table>
    <tr>
      <td>aaaa</td>
      <td>bbbb</td>
      <td>cccc</td>
      <td>eeee</td>
    </tr>
    <tr>
      <td>aaaa</td>
      <td>bbbb</td>
      <td>cccc</td>
      <td>eeee</td>
    </tr>
    <tr>
      <td>aaaa</td>
      <td>bbbb</td>
      <td>cccc</td>
      <td>eeee</td>
    </tr>
    <tr>
      <td>aaaa</td>
      <td>bbbb</td>
      <td>cccc</td>
      <td>eeee</td>
    </tr>    
  </table>
</div>

【讨论】:

  • 这将适用于 Skydiver,但除非您有特定原因对 JavaScript 中的悬停事件做出反应,否则您可以简单地使用 CSS 设置悬停样式。我建议将 Alexanders 示例与 abhiklpms 结合使用。您需要在单击事件上设置行背景颜色以永久设置颜色,如果这是您想要的。
【解决方案2】:

这是一个仅使用 css 的演示,单击并按住该行以查看单击时的颜色。

.tftable {
    font-size:12px;
    color:#333333;
    width:100%;
    border-width: 1px;
    border-color: #729ea5;
    border-collapse: collapse;
}
.tftable th {
    font-size:12px;
    background-color:#acc8cc;
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #729ea5;
    text-align:left;
}
.tftable tr {
    background-color:#d4e3e5;
}
.tftable td {
    font-size:12px;
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #729ea5;
}

/*  Define the background color for all the ODD background rows  */
 .tftable tr:nth-child(odd) {
    background: #b8d1f3;
}
/*  Define the background color for all the EVEN background rows  */
 .tftable tr:nth-child(even) {
    background: #dae5f4;
}
.tftable tr:hover {
    background-color:#ffffff;
}
.tftable tr:active {
    background-color:#ccc;
}
<table class="tftable" border="1">
    <tr>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
        <th>Header 4</th>
        <th>Header 5</th>
    </tr>
    <tr>
        <td>Row:1 Cell:1</td>
        <td>Row:1 Cell:2</td>
        <td>Row:1 Cell:3</td>
        <td>Row:1 Cell:4</td>
        <td>Row:1 Cell:5</td>
    </tr>
    <tr>
        <td>Row:2 Cell:1</td>
        <td>Row:2 Cell:2</td>
        <td>Row:2 Cell:3</td>
        <td>Row:2 Cell:4</td>
        <td>Row:2 Cell:5</td>
    </tr>
    <tr>
        <td>Row:3 Cell:1</td>
        <td>Row:3 Cell:2</td>
        <td>Row:3 Cell:3</td>
        <td>Row:3 Cell:4</td>
        <td>Row:3 Cell:5</td>
    </tr>
    <tr>
        <td>Row:4 Cell:1</td>
        <td>Row:4 Cell:2</td>
        <td>Row:4 Cell:3</td>
        <td>Row:4 Cell:4</td>
        <td>Row:4 Cell:5</td>
    </tr>
    <tr>
        <td>Row:5 Cell:1</td>
        <td>Row:5 Cell:2</td>
        <td>Row:5 Cell:3</td>
        <td>Row:5 Cell:4</td>
        <td>Row:5 Cell:5</td>
    </tr>
    <tr>
        <td>Row:6 Cell:1</td>
        <td>Row:6 Cell:2</td>
        <td>Row:6 Cell:3</td>
        <td>Row:6 Cell:4</td>
        <td>Row:6 Cell:5</td>
    </tr>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多