Hide Twitter Bootstrap left and right arrows if only one slider is present

I tried to hide the left and right buttons of Bootstrap Carousel if there is only one slider, but cannot make it work correctly.

I tried

    if ($('.carousel-inner div').length === 1 ) { 
        $(this).find('.controls .carousel-control').hide();
    }

and

    if($('.carousel-inner .item').is(':only-child')) {
    $(this).find('.controls .carousel-control').hide();

No luck.

Is there any special reason why none of them will work? The console will return the correct number of sliders for .length, and I use the same method .hidefor another function related to this carousel.

+5
source share
2 answers

Instead $(this).find(...).hide();try$('.controls .carousel-control').hide();

+6
source

, 1 . , , :

Bootstrap:

<div id="myCarousel" class="carousel slide">
  <ol class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ol>
  <!-- Carousel items -->
  <div class="carousel-inner">
    <div class="active item"></div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
  <!-- Carousel nav -->
  <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
  <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
</div>

, $(this) :

$('.carousel-inner').each(function() {

    if ($(this).children('div').length === 1) $(this).siblings('.carousel-control, .carousel-indicators').hide();

});
+12

All Articles