简体   繁体   中英

check on last section element

how can check on the last section element. I have a lot of sections on the page. I want check on the last. I have this

if ($("section:last")) {
Button.hide();
} else {
Button.animate():
}

But this is not working...

Say you want to do something only to the last element that satisfies a criteria then:

  $(".section:last").css("background-color", "red");

would set it red. You do not need the if statement for this.

If your function need to go through all sections and you need to know if you're at the last section, you can put all sections in class="section" and try this way:

<script type="text/javascript">
    var len = $('.section').length;
    $('.section').each(function(index, element) {
        if (index == len - 1) {
          // This is the last element, do what you need to do with it here
        } else {
          // This is not the last element, do something else
        }
    }
</script>

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