How to select first child with jQuery?

前端 未结 5 970
暗喜
暗喜 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:39

    Hi we can use default method "first" in jQuery

    Here some examples:

    When you want to add class for first div

      $('.alldivs div').first().addClass('active');
    

    When you want to change the remove the "onediv" class and add only to first child

       $('.alldivs div').removeClass('onediv').first().addClass('onediv');
    

提交回复
热议问题