Get second child using jQuery

前端 未结 9 1445
灰色年华
灰色年华 2020-12-12 15:03
$(t).html()

returns

test1test2

I want to retrieve the second td<

相关标签:
9条回答
  • 2020-12-12 15:59

    grab the second child:

    $(t).children().eq(1);
    

    or, grab the second child <td>:

    $(t).children('td').eq(1);
    
    0 讨论(0)
  • 2020-12-12 16:06

    Here's a solution that maybe is clearer to read in code:

    To get the 2nd child of an unordered list:

       $('ul:first-child').next()
    

    And a more elaborated example: This code gets the text of the 'title' attribute of the 2nd child element of the UL identified as 'my_list':

       $('ul#my_list:first-child').next().attr("title")
    

    In this second example, you can get rid of the 'ul' at the start of the selector, as it's redundant, because an ID should be unique to a single page. It's there just to add clarity to the example.

    Note on Performance and Memory, these two examples are good performants, because they don't make jquery save a list of ul elements that had to be filtered afterwards.

    0 讨论(0)
  • 2020-12-12 16:06

    Try this:

    $("td:eq(1)", $(t))
    

    or

    $("td", $(t)).eq(1)
    
    0 讨论(0)
提交回复
热议问题