How to select first child with jQuery?

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

    As @Roko mentioned you can do this in multiple ways.

    1.Using the jQuery first-child selector - SnoopCode

       $(document).ready(function(){
        $(".alldivs onediv:first-child").css("background-color","yellow");
            }
    
    1. Using jQuery eq Selector - SnoopCode

       $( "body" ).find( "onediv" ).eq(1).addClass( "red" );
      
    2. Using jQuery Id Selector - SnoopCode

         $(document).ready(function(){
           $("#div1").css("background-color: red;");
         });
      
    0 讨论(0)
  • 2020-11-28 08:30

    Use the :first-child selector.

    In your example...

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

    This will also match any first child descendents that meet the selection criteria.

    While :first matches only a single element, the :first-child selector can match more than one: one for each parent. This is equivalent to :nth-child(1).

    For the first matched only, use the :first selector.

    Alternatively, Felix Kling suggested using the direct descendent selector to get only direct children...

    $('div.alldivs > div:first-child')
    
    0 讨论(0)
  • 2020-11-28 08:31

    Try with: $('.onediv').eq(0)

    demo jsBin

    From the demo: Other examples of selectors and methods targeting the first LI unside an UL:

    .eq() Method: $('li').eq(0)
    :eq() selector: $('li:eq(0)')
    .first() Method $('li').first()
    :first selector: $('li:first')
    :first-child selector: $('li:first-child')
    :lt() selector:$('li:lt(1)')
    :nth-child() selector:$('li:nth-child(1)')

    jQ + JS:

    Array.slice() Method: $('li').slice(0,1)

    you can also use [i] to get the JS HTMLelement index out of the jQuery el. (array) collection like eg:

    $('li')[0]
    

    now that you have the JS element representation you have to use JS native methods eg:

    $('li')[0].className = 'active'; // Adds class "active" to the first LI in the DOM
    

    or you can (don't - it's bad design) wrap it back into a jQuery object

    $( $('li')[0] ).addClass('active'); // Don't. Use .eq() instead
    
    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题