【问题标题】:CSS Table Row border color with border-collapseCSS 表格行边框颜色与边框折叠
【发布时间】:2011-12-18 00:54:49
【问题描述】:

我在这里看过几篇关于这个主题的帖子,我已经阅读了 W3C 关于边框样式冲突解决的规范(承认,我不完全明白),但我不知道如何实现我想要的是。

在行悬停时,我想更改行周围边框的颜色。我推测最好的跨浏览器方法是更改​​该行中的 td 边框颜色。但是,我似乎无法以行的顶部边框也发生变化的方式执行它。

这是我的 CSS:

#dataGrid table {
border: 1px solid #cacac8; /* I've tried it with and without this border setting */
table-layout: fixed;
border-collapse: collapse;
}

#dataGrid td {
    border: 1px solid #cacac8;
    padding: 8px 11px 7px 11px;
    text-align: left;
}

#dataGrid .cellHovered {
    border-top: 1px solid #425474;
    border-bottom: 1px solid #425474;
}

#dataGrid .cellFirstHovered {border-left: 1px solid #425474;}
#dataGrid .cellLastHovered {border-right: 1px solid #425474;}

还有我的 JQuery:

$('div#dataGrid tr.dataRow').hover(
        function () {
            $(this).children('td').addClass('cellHovered');
            $(this).children('td:first').addClass('cellFirstHovered');
            $(this).children('td:last').addClass('cellLastHovered');
        },
        function() {
            $(this).children('td').removeClass('cellHovered');
            $(this).children('td:first').removeClass('cellFirstHovered');
            $(this).children('td:last').removeClass('cellLastHovered');
    });

【问题讨论】:

    标签: jquery css row border css-tables


    【解决方案1】:

    首先,最好不要使用 jQuery,而是使用纯 CSS:

    #datagrid tr.datarow:hover td {
        border: whatever;
    }
    

    接下来,由于您使用的是 1px 边框,请尝试以下技巧:

    #datagrid tr.datarow:hover td {
        border-style: double;
    }
    

    由于doublesolid“更独特”,它的颜色优先于它周围的单元格,并且看起来与solid 相同;)

    【讨论】:

    • 我将成为一名太阳枪。效果很好。非常感谢。碰巧知道它是否适用于 IE 7?
    • 据我所知,是的,它工作正常。至少,它可以在 IE9 的 IE7 模式下运行(根据我的经验,这似乎相当可靠)。然而,IE6 不会喜欢它,但没关系:p
    • 嗯...这会在行中的每个单元格周围设置边框。如果我用 CSS 悬停省略了 td,并且只在行周围放置了一个边框,那么这适用于 IE 7 吗? (或者任何类型的 IE,就此而言。
    • 不确定,但您可以在tds 上使用:first-child:last-child 选择器来做侧边框,然后让其余部分只做顶部和底部。那行得通。
    • 没问题,很高兴我能帮上忙 :)