【问题标题】:Add background color and border to table row on hover using jquery使用jquery在悬停时为表格行添加背景颜色和边框
【发布时间】:2009-03-27 01:47:48
【问题描述】:

有没有人知道当鼠标悬停在行上时可以为具有不同背景颜色的表格行添加边框?

我已经能够用这个改变行的背景颜色:

$(document).ready(function() {
   $(function() {
        $('.actionRow').hover(function() {
            $(this).css('background-color', '#FFFF99');
        },
        function() {
            $(this).css('background-color', '#FFFFFF');
        });
    });
});

但我无法添加边框颜色。我意识到边框不能直接应用于表格行标签,但我希望有人知道一些 jQuery 巫术魔法,可以在所选行中找到表格单元格并将一些样式应用于这些单元格以创建边框。

谢谢!

【问题讨论】:

    标签: jquery css


    【解决方案1】:
       $(function() {
            $('tr').hover(function() {
                $(this).css('background-color', '#FFFF99');
                $(this).contents('td').css({'border': '1px solid red', 'border-left': 'none', 'border-right': 'none'});
                $(this).contents('td:first').css('border-left', '1px solid red');
                $(this).contents('td:last').css('border-right', '1px solid red');
            },
            function() {
                $(this).css('background-color', '#FFFFFF');
                $(this).contents('td').css('border', 'none');
            });
        });
    

    【讨论】:

    • 在 IE7 中有点松鼠。数字。
    【解决方案2】:

    您最好的选择是在行上添加类和删除类。 然后在你的样式表中:

    .actionRow-hovered td { border-color: whatever; }
    

    因此,您实际上将操作每个 td 边框颜色。 很痛苦,但是当你掌握了它的窍门时效果很好。

    【讨论】:

      【解决方案3】:
      $('table.tblMenuTabls tr').hover(function(){
              $(this).toggleClass('tblOverRow');
              },function(){
                  $(this).toggleClass('tblOverRow')}
          ).css({cursor: 'hand'});
      

      tblMenuTabls 是表类名称,tblOverRow 是带有悬停定义的 CSS 类。

      【讨论】:

        【解决方案4】:

        【讨论】:

        • 这是针对整个表格边框,而不是单个行。
        猜你喜欢
        • 2011-12-01
        • 1970-01-01
        • 2013-06-28
        • 1970-01-01
        • 1970-01-01
        • 2013-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多