html表格如何通过更改悬停边框来突出显示列?

2022-01-22 00:00:00 html-table hover javascript html css

我正在探索如何设置表格样式,以便在鼠标悬停在列上时可以更改边框.

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.

相关文章