Get second child using jQuery

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

returns

test1test2

I want to retrieve the second td<

9条回答
  •  青春惊慌失措
    2020-12-12 15:57

    You can use two methods in jQuery as given below-

    Using jQuery :nth-child Selector You have put the position of an element as its argument which is 2 as you want to select the second li element.

    $( "ul li:nth-child(2)" ).click(function(){
      //do something
    });

    Using jQuery :eq() Selector

    If you want to get the exact element, you have to specify the index value of the item. A list element starts with an index 0. To select the 2nd element of li, you have to use 2 as the argument.

    $( "ul li:eq(1)" ).click(function(){
      //do something
    });

    See Example: Get Second Child Element of List in jQuery

提交回复
热议问题