How to select first child with jQuery?

前端 未结 5 998
暗喜
暗喜 2020-11-28 08:25

How do I select the first div in these divs (the one with id=div1) using first child selectors?

5条回答
  •  广开言路
    2020-11-28 08:52

    $('div.alldivs :first-child');
    

    Or you can just refer to the id directly:

    $('#div1');
    

    As suggested, you might be better of using the child selector:

    $('div.alldivs > div:first-child')
    

    If you dont have to use first-child, you could use :first as also suggested, or $('div.alldivs').children(0).

提交回复
热议问题