jQuery Select first and second td

后端 未结 7 1362
南旧
南旧 2020-12-05 04:21

How can I add a class to the first and second td in each tr?

THIS ONE
相关标签:
7条回答
  • 2020-12-05 04:32

    jquery provides one more function: eq

    Select first tr

    $(".bootgrid-table tr").eq(0).addClass("black");

    Select second tr

    $(".bootgrid-table tr").eq(1).addClass("black");

    0 讨论(0)
  • 2020-12-05 04:34

    You can do in this way also

    var prop = $('.someProperty').closest('tr');
    

    If the number of tr is in array

    $.each(prop , function() {
      var gotTD = $(this).find('td:eq(1)');                 
    });
    
    0 讨论(0)
  • 2020-12-05 04:36
    $(".location table tbody tr").each(function(){
        $('td:first', this).addClass('black').next().addClass('black');
    });
    

    another:

    $(".location table tbody tr").find('td:first, td:nth-child(2)').addClass('black');
    
    0 讨论(0)
  • 2020-12-05 04:40

    If you want to add a class to the first and second td you can use .each() and slice()

    $(".location table tbody tr").each(function(){
        $(this).find("td").slice(0, 2).addClass("black");
    });
    

    Example on jsfiddle

    0 讨论(0)
  • 2020-12-05 04:45

    To select the first and the second cell in each row, you could do this:

    $(".location table tbody tr").each(function() {
        $(this).children('td').slice(0, 2).addClass("black");
    });
    
    0 讨论(0)
  • 2020-12-05 04:46

    You can just pick the next td:

    $(".location table tbody tr td:first-child").next("td").addClass("black");
    
    0 讨论(0)
提交回复
热议问题