简体   繁体   中英

Check if element is last of the class in the DOM

I have this code:

$('.bird').each(function(){
    $(this).load(function(){
      if($(this).is(".bird:last")){
        $('#floatingBarsG').remove();
        $('#step0').fadeIn(600);}
    });
})

Where I try to do some events if $(this) is last element on the page with class .bird , and this code doesn't work.

What is wrong?

Try

var $birds = $('.bird').each(function () {
    $(this).load(function () {
        if ($birds.last().is(this)) {
            $('#floatingBarsG').remove();
            $('#step0').fadeIn(600);
        }
    });
})

DEMO

$('.bird').click(function () {
    if ($('.bird').length - 1 == $(this).index('.bird')) {
        alert('last')
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM