get row by index

前端 未结 9 913
名媛妹妹
名媛妹妹 2020-12-17 16:35

how can you get a row by the index?

var rows = $(\'tr\', tbl);
rows.index(0).addClass(\'my_class\');
相关标签:
9条回答
  • 2020-12-17 17:05

    You can use nth-child in your selector:

    $('tr td:nth-child(3)').addClass('my_class');
    

    Will get the third td.

    0 讨论(0)
  • 2020-12-17 17:05

    Use eq()

    $('tr', tbl).eq(0).addClass('my_class');
    
    0 讨论(0)
  • 2020-12-17 17:07

    http://api.jquery.com/get/ says:

    Retrieve the DOM elements matched by the jQuery object.
    .get( [index] )
    index A zero-based integer indicating which element to retrieve.

    Note that you'll get the DOM object, not a jQuery one:

    var rows = $('tr', tbl);
    $(rows.get(0)).addClass('my_class');
    
    0 讨论(0)
  • 2020-12-17 17:12

    you can do

    $('tr:eq(0)', tbl).addClass('my_class');
    

    more on this http://api.jquery.com/eq-selector/

    0 讨论(0)
  • 2020-12-17 17:13
    var row=$('tr:eq(5)', tbl);  // returns the 5th row
    
    0 讨论(0)
  • 2020-12-17 17:18

    You could use the native rows[docs] property on the HTMLTableElement.

    $(tbl[0].rows[0]).addClass('my_class');
    

    As noted by @Felix, I've assumed that tbl is a jQuery object. If not, do this:

    $(tbl.rows[0]).addClass('my_class');
    
    0 讨论(0)
提交回复
热议问题