Get second child using jQuery

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

returns

test1test2

I want to retrieve the second td<

9条回答
  •  孤街浪徒
    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.

提交回复
热议问题