代码之家  ›  专栏  ›  技术社区  ›  svk

如何使用Jquery突出显示表中选定的行?

  •  1
  • svk  · 技术社区  · 15 年前

    我的密码是 this

    3 回复  |  直到 15 年前
        1
  •  3
  •   Logan Bailey    15 年前
    $(document).ready(function()
    {
        $('td input[type="checkbox"]').click(function(){
            if ($(this).is(':checked')){
                  $(this).parent().addClass('highlighted');
                  $(this).parent().siblings().addClass('highlighted');
            } else if($(this).parent().is('.highlighted')) {
                 $(this).parent().removeClass('highlighted');
                 $(this).parent().siblings().removeClass('highlighted');
            }
        });
    });
    

    有一些不同的方法来突出显示行。但我刚刚添加了一个名为highlighted的类,它有一个背景色,并将其设置为所有tds

        2
  •  1
  •   JeremyWeir    15 年前

    我想你想要这样的东西

    $('input[type=checkbox]').click(function(){ 
        var cb = $(this);
        var tr = cb.closest('tr');
        cb.attr('checked') ? tr.addClass('highlight') : tr.removeClass('highlight');
    });
    

    .highlight .tableeven { background-color: #f00; }
    .highlight .tableodd { background-color: #c00; } 
    
        3
  •  0
  •   sTodorov    15 年前

    或许是这样:

    $('table input[type=checkbox]').click(function(){
    if($(this).is(':checked'))
    $(this).parents('tr :first').addClass('myStyleClass');
    else
    $(this).parents('tr :first').removeClass('myStyleClass');
    });