How can I find the last “li” in a “ul” using jQuery?

后端 未结 5 2038
南方客
南方客 2021-01-04 18:15

I want to check the li that is the last li in ul. How can I check that using jQuery?

相关标签:
5条回答
  • 2021-01-04 19:03

    Just use the following

    if($('#ulscroller li:last-child')[0] == liTocheck)
    {
       alert("This is the last li");
    }
    

    Or

    if($('#ulscroller li').last()[0] == liTocheck)
    {
       alert("This is the last li");
    }
    

    Here liTocheck is the li that needs to be compared

    0 讨论(0)
  • 2021-01-04 19:04

    You can use the .last() matcher

    $('#ulscroller li').last()
    
    0 讨论(0)
  • 2021-01-04 19:09

    Just use the :last-child selector :

    $('#ulscroller li:last-child')
    

    DEMO: http://jsfiddle.net/f5v6R/

    For example, if you want to know if it has the selected class you may do

    if ($('#ulscroller li:last-child').hasClass('selected')) {
       // do something
    }
    
    0 讨论(0)
  • 2021-01-04 19:17
    $('ul#ulscroller').children('li').last();
    

    http://api.jquery.com/last/

    You could also do it like so:

    $('ul#ulscroller').children('li:last-child');
    

    http://api.jquery.com/last-selector/

    Here's an example to illustrate it: http://jsfiddle.net/TheNix/W92vF/

    0 讨论(0)
  • 2021-01-04 19:19

    try this

    $(function(){
        alert($('#ulscroller li:last-child').val());
    })
    
    0 讨论(0)
提交回复
热议问题