How can a html table highlight columns by changing the border on hover?(html表格如何通过更改悬停边框来突出显示列?)
问题描述
我正在探索如何设置表格样式,以便在鼠标悬停在列上时可以更改边框.
I'm exploring how to style a table in such a way that the border can be changed when the mouse hovers over a column.
当鼠标悬停在一列上时,我想通过更改边框颜色来突出显示该列:
When the mouse is over a column, I want to highlight that column by changing the border color:
为了突出显示,我在 jQuery 库中使用了以下 JavaScript 代码:
To highlight, I am using the following JavaScript code with the jQuery library:
$('td').hover(function() {
var t = parseInt($(this).index()) + 1;
$('td:nth-child(' + t + ')').addClass('highlighted');
},
function() {
var t = parseInt($(this).index()) + 1;
$('td:nth-child(' + t + ')').removeClass('highlighted');
});
使用以下 CSS:
.highlighted {
border-top: 2px solid black;
border-right: 2px solid black;
border-left: 2px solid black;
}
您可以在此 JSFiddle 中查看其工作原理:http://jsfiddle.net/sCFjj/1/这仅在我将鼠标悬停在最左侧的列上时才有效.否则,它会突出显示右列(和顶部),但不会突出显示左垂直列.有没有办法让左竖列出现?
You can view how this works in this JSFiddle: http://jsfiddle.net/sCFjj/1/ This is only working when I hover on the left-most column. Otherwise it highlights the right-column (and top) but not the left vertical column. Is there a way of making the left-vertical column appear?
理想情况下,我希望效果忽略最低行,但我不确定如何从 jQuery 选择中排除一行.
Ideally, I would like the effect to ignore the lowest row, but I am not sure how to exclude a row from the jQuery selection.
我的尝试基于之前的问题.
推荐答案
链接:jsFiddle.还添加到上一列border-right,你会得到你想要的.我认为该列的右边界在下一个列的左边界之上.
Link: jsFiddle. Also add to previous collumn border-right and you will get that you want. I think that collumn's right border is over next collumn left border.
JavaScript:
JavaScript:
$('td').hover(function() {
var t = parseInt($(this).index()) + 1;
$('td:nth-child(' + t + ')').addClass('highlighted');
if(t>1){
var t1 = t -1;
//alert("T:" + t + "<br/> T1:" + t1);
$('td:nth-child(' + t1 + ')').addClass('highlightedPrev');
}
},
function() {
var t = parseInt($(this).index()) + 1;
$('td:nth-child(' + t + ')').removeClass('highlighted ');
if(t>1){
var t1 = t -1;
$('td:nth-child(' + t1 + ')').removeClass('highlightedPrev');
}
});
CSS:
.highlighted {
border: 2px solid black;
}
.highlightedPrev {
border-right: 2px solid black;
}
希望我解决了你的问题.
Hope I solved your problem.
这篇关于html表格如何通过更改悬停边框来突出显示列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:html表格如何通过更改悬停边框来突出显示列?
基础教程推荐
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01