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表格如何通过更改悬停边框来突出显示列?


- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01