简体   繁体   English

桌子上的边框悬停问题<tr>

[英]Issue with Border Hover on Table <tr>

I am having trouble figuring out what I'm doing wrong... I'm trying to do something that should be simple--change the border color of an entire row when the user hovers over the row. 我在弄清楚自己在做什么错时遇到了麻烦...我试图做一些应该很简单的事情-当用户将鼠标悬停在整行上时,更改整行的边框颜色。

For the table, I'm using the following CSS code: 对于该表,我正在使用以下CSS代码:

  .sch{ border-collapse: collapse; width:97%; margin: 0 auto; margin-top:30px; } .sch tr{ border: 2px solid #000000; } .sch tr:hover{ border-color: red; } 
  <table class='sch'> <tr><td>Test</td><td>Test</td></tr> <tr><td>Test</td><td>Test</td></tr> <tr><td>Test</td><td>Test</td></tr> </table> 

The issue is that, when you hover over the second or third row, the top bar of the border remains black, while the sides and bottom change to red. 问题是,当您将鼠标悬停在第二行或第三行上时,边框的顶部栏保持黑色,而侧面和底部变为红色。 Only the top row changes to red all the way around. 始终只有第一行变成红色。

I suspect that this has to do with the bottom of the previous row somehow covering up the red of the hover, but I've tried about everything--except the right answer---to fix it. 我怀疑这与上一行的底部以某种方式掩盖了悬停的红色有关,但是我已经尝试了所有方法(正确答案除外)来解决此问题。

Thanks for your help! 谢谢你的帮助!

border-collapse: collapse; is a culprit here. 是这里的元凶。

It is related to the fact that the top cell bottom border is on top of the bottom cell top border. 这与以下事实有关:顶部单元格底部边框位于底部单元格顶部边框的顶部。 If you make top cell bottom border as none then you will see all borders properly being set to red. 如果将顶部单元格的底部边框设为无,则将看到所有边框都正确设置为红色。

Look at this interactive example in MDN to see exactly what happens https://developer.mozilla.org/en-US/docs/Web/CSS/border-collapse 查看MDN中的此交互式示例,以确切了解发生了什么https://developer.mozilla.org/en-US/docs/Web/CSS/border-collapse

This appears to be tricky to implement without JavaScript. 如果没有JavaScript,实现起来似乎很棘手。 This is a solution using jQuery: 这是使用jQuery的解决方案:

 $(".sch tr").hover(function(){ $(this).css("border-color", "red"); $(this).prev().css("border-bottom-width", "0"); }, function(){ $(this).css("border-color", "#000000"); $(this).prev().css("border-bottom-width", "2px"); }); 
 .sch{ border-collapse: collapse; width:97%; margin: 0 auto; margin-top:30px; } .sch tr{ border: 2px solid #000000; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class='sch'> <tr><td>Test</td><td>Test</td></tr> <tr><td>Test</td><td>Test</td></tr> <tr><td>Test</td><td>Test</td></tr> </table> 

If you want to use CSS and HTML only, you can use such not the best but working solution. 如果只想使用CSS和HTML,则可以使用并非最佳但可行的解决方案。

CSS-file: CSS文件:

.sch {
    width:97%;
    margin: 0 auto;
    margin-top:30px;
    border-collapse: collapse;
}
tr {
    border: 2px solid #000000;
}
.sch tr:hover {
    border-color: red;
    border-bottom-width: 2px;
}
tr:nth-child(3) {
    border-bottom-width: 0;
}
tr:nth-last-child(1) {
    border-bottom-width: 2px;
}

HTML-file: HTML文件:

<table class='sch'>
    <tr>
        <td>Test</td><td>Test</td>
    </tr>
    <tr>
        <td>Test</td><td>Test</td>
    </tr>
    <tr>
        <td>Test</td><td>Test</td>
    </tr>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM