• First...">
    All geek questions in one place

    JQuery selector to find the first child in each element set

    I have this HTML:

    <div id="tags">
        <ul>
            <li><input type="text" /><a href="#">First Link</a> <a href="#">Second Link</a></li>
            <li><input type="text" /><a href="#">First Link</a> <a href="#">Second Link</a></li>
            <li><input type="text" /><a href="#">First Link</a> <a href="#">Second Link</a></li>
            <li><input type="text" /><a href="#">First Link</a> <a href="#">Second Link</a></li>
            <li><input type="text" /><a href="#">First Link</a> <a href="#">Second Link</a></li>
        </ul>
    </div>
    

    Which selector would I use to grab the first anchor of each li element, and what about the second binding of each li without adding additional identifiers or classes?

    Well, I tried this:

    $('#tags a:last-child')
    

    And I was able to get the second binding of each li, but I do not understand why this works. Shouldn't the element be inside the anchor in order to select something else, but it can select the second anchor of each li. Later, I didn’t care how it worked while it was working, so I suppose I will do the same to get the first element, which would be the following:

    $('#tags a:first-child')
    

    However, this does not work to get the first binding of each li. Any ideas?

    EDIT: , , , , , , , . , ?

    +3
    jquery selector
    Joker 19 . '11 21:05
    4

    ": first/last-child" ( "" ) , , , .

    , $('#tags a:last-child') , .

    $('#tags a:first-child') , .

    $('#tags a:not(:last-child)'), , , , , , , .

    $('#tags a:nth-child(2)'), , , , 2- , , , .

    ( , , . , , , . , :))

    +3
    Ilia Draznin 19 . '11 22:28

    li

    $("#tags li a:first-child") $("#tags li a:nth-child(0)")

    li - ?

    $("#tags li a:last-child") $("#tags li a:nth-child(1)")

    , :*-child, , a, .

    a :first-child = > a

    a:first-child = > a


    : http://jsfiddle.net/7vupb/

    +5
    hunter 19 . '11 21:10
    $('#tags li').find('a:first') ...
    

    You can also do something like:

    $('#tags li').find('a').each(function(i){
       if(i==0){ //first item
    
       }
       if(i==1){ //second item
    
       }
    });
    
    +2
    Jage Jan 19 '11 at 21:07
    source share

    Is this what you want:

    $('#tags a:nth-of-type(1)')
    

    This will select all the "a" children of the "#tags" element, which are the first child anchor of the parent. Just use 2 instead of 1 for the second anchors.

    +1
    Jhacker Jul 01 '15 at 23:34
    source share

    Source: https://habr.com/ru/post/1786503/

    More articles:

    • database - smooth and normalized - database
    • Difference between Satchmo local_settings.py and settings.py - python
    • Ajax views with django - ajax
    • Do these two statements have the same thing? - c #
    • Look for an alternative solution for generating a data sequence - c ++
    • What does this JS expression mean? - javascript
    • An increment of an integer at the end of a string - string
    • FullCalendar: can I get the start time associated with EventClick coordinates? - javascript
    • Javascript endless prototype chain - javascript
    • Continuous Integration with .Net and Mono, Linux and Windows - .net

    All Articles

    Geek Questions | 2019