最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Jquery hide first 12 elementes, show next 12 elements - Stack Overflow

matteradmin8PV0评论

what i am trying to do is make the first 12 elements hidden and show the next 12 elements.

//this is dynamic loaded content
<div class="inner-content">
     <div class="front-pro">1</div>
     <div class="front-pro">2</div>
     <div class="front-pro">3</div>
     <div class="front-pro">4</div>
     <div class="front-pro">5</div>
     <div class="front-pro">6</div>
     <div class="front-pro">7</div>
     <div class="front-pro">8</div>
     <div class="front-pro">9</div>
     <div class="front-pro">10</div>
     <div class="front-pro">11</div>
     <div class="front-pro">12</div>
     <div class="front-pro hidden">13</div>
     <div class="front-pro hidden">14</div>
     ..... etc (200 divs more)
</div>
<div onclick="SearchNext();">next</div>

This is my javascript/jquery:

function SearchNext(){
 var first = $('.inner-content').children('.front-pro:hidden:first');
    first.prevAll(':lt(12)').hide();
    first.nextAll(':lt(12)').show();
}

It works one time, after it stops working. (and it skips number 13) i want to have 12 new elements visible with each Next click and hide the previous.

UPDATE - this is my end result that works perfectly JSFIDDLE DEMO

Thanks to Alex Char

PHP for creating page numbers, you could do this also with javascript

//$counter is search results
    $x = 1; 
    $Pnumbers = '';
    while($x <= ceil($counter/12)) {
        if($x == 1){ $ecl = 'bold'; } else{ $ecl = ''; }
       $Pnumbers .= ' <span class="number '.$ecl.' numbering" onClick="GoTo('.$x.');">'.$x.'</span> ';
        $x++;
    } 

    if($counter > 12){ echo'<div class="page-numbers">
    <span class="prev number" onclick="GoTo(\'prev\')">Prev</span>
    '.$Pnumbers.'
    <span class="next number" onclick="GoTo(\'next\');">Next</span>
    </div>'; }

Javascript:

function GoTo(nn) {
      var nng = parseInt($('.page-numbers .numbering.bold').text());
     if(nn == 'next'){
        nn = nng+1; 
     }if(nn == 'prev'){
         nn = nng-1; 
     } 
    //get all child elements with class front-pro 
    //of element with class .inner-content
    var childElems = $(".inner-content .front-pro");
    var totalpages = Math.ceil(childElems.length/12);
    //iterate through the elements
    var first = (nn-1)*12;
    var last = first+11;
    childElems.each(function(i, el) {

      //show the elements that match the criteria removing css class
      if (i >= first && i <= last) {
        $(el).removeClass('hidden');
      } else {
        //hide rest
        $(el).addClass('hidden');
      }
    });

    if(nn > 1){ $('.page-numbers .prev').show(); }else{ $('.page-numbers .prev').hide(); }
    if(nn < totalpages){ $('.page-numbers .next').show(); }else{ $('.page-numbers .next').hide(); }
    $('.page-numbers .numbering').removeClass('bold');
    $('.page-numbers .numbering:eq('+(nn-1)+')').addClass('bold');
  }

CSS:

.front-pro.hidden{ display:none !important; }
.prev {  display: none; }
.page-numbers .number{
background: #ff0000;  }
.page-numbers{ text-align:center; }
.page-numbers .number.bold{ font-weight:bold; background:#000; }
.page-numbers .number:hover{ background:#000; cursor: pointer; }

Make sure that the first 12 divs do not contain the "hidden" class, all the divs that e after should have "hidden" in there class

what i am trying to do is make the first 12 elements hidden and show the next 12 elements.

//this is dynamic loaded content
<div class="inner-content">
     <div class="front-pro">1</div>
     <div class="front-pro">2</div>
     <div class="front-pro">3</div>
     <div class="front-pro">4</div>
     <div class="front-pro">5</div>
     <div class="front-pro">6</div>
     <div class="front-pro">7</div>
     <div class="front-pro">8</div>
     <div class="front-pro">9</div>
     <div class="front-pro">10</div>
     <div class="front-pro">11</div>
     <div class="front-pro">12</div>
     <div class="front-pro hidden">13</div>
     <div class="front-pro hidden">14</div>
     ..... etc (200 divs more)
</div>
<div onclick="SearchNext();">next</div>

This is my javascript/jquery:

function SearchNext(){
 var first = $('.inner-content').children('.front-pro:hidden:first');
    first.prevAll(':lt(12)').hide();
    first.nextAll(':lt(12)').show();
}

It works one time, after it stops working. (and it skips number 13) i want to have 12 new elements visible with each Next click and hide the previous.

UPDATE - this is my end result that works perfectly JSFIDDLE DEMO

Thanks to Alex Char

PHP for creating page numbers, you could do this also with javascript

//$counter is search results
    $x = 1; 
    $Pnumbers = '';
    while($x <= ceil($counter/12)) {
        if($x == 1){ $ecl = 'bold'; } else{ $ecl = ''; }
       $Pnumbers .= ' <span class="number '.$ecl.' numbering" onClick="GoTo('.$x.');">'.$x.'</span> ';
        $x++;
    } 

    if($counter > 12){ echo'<div class="page-numbers">
    <span class="prev number" onclick="GoTo(\'prev\')">Prev</span>
    '.$Pnumbers.'
    <span class="next number" onclick="GoTo(\'next\');">Next</span>
    </div>'; }

Javascript:

function GoTo(nn) {
      var nng = parseInt($('.page-numbers .numbering.bold').text());
     if(nn == 'next'){
        nn = nng+1; 
     }if(nn == 'prev'){
         nn = nng-1; 
     } 
    //get all child elements with class front-pro 
    //of element with class .inner-content
    var childElems = $(".inner-content .front-pro");
    var totalpages = Math.ceil(childElems.length/12);
    //iterate through the elements
    var first = (nn-1)*12;
    var last = first+11;
    childElems.each(function(i, el) {

      //show the elements that match the criteria removing css class
      if (i >= first && i <= last) {
        $(el).removeClass('hidden');
      } else {
        //hide rest
        $(el).addClass('hidden');
      }
    });

    if(nn > 1){ $('.page-numbers .prev').show(); }else{ $('.page-numbers .prev').hide(); }
    if(nn < totalpages){ $('.page-numbers .next').show(); }else{ $('.page-numbers .next').hide(); }
    $('.page-numbers .numbering').removeClass('bold');
    $('.page-numbers .numbering:eq('+(nn-1)+')').addClass('bold');
  }

CSS:

.front-pro.hidden{ display:none !important; }
.prev {  display: none; }
.page-numbers .number{
background: #ff0000;  }
.page-numbers{ text-align:center; }
.page-numbers .number.bold{ font-weight:bold; background:#000; }
.page-numbers .number:hover{ background:#000; cursor: pointer; }

Make sure that the first 12 divs do not contain the "hidden" class, all the divs that e after should have "hidden" in there class

Share Improve this question edited Mar 30, 2016 at 15:15 Vazzilly asked Mar 29, 2016 at 16:10 VazzillyVazzilly 1812 silver badges10 bronze badges 1
  • Glad that I helped you. Looks pretty good. One suggestion. Instead of using only one function(GoTo) think about to use smaller functions or you can use Revealing Module Pattern as I did in my example. – Alex Char Commented Mar 30, 2016 at 15:37
Add a ment  | 

3 Answers 3

Reset to default 6

I change a bit the implementation to support and previous. I use a css class to hide content.

function searchNext() {
  $('.inner-content').children('.front-pro:lt(12)').addClass('hidden');
  $('.inner-content').children('.front-pro:gt(11)').removeClass('hidden');
  $(".next").hide();
  $(".prev").show();
}

function searchPrev() {
  $('.inner-content').children('.front-pro:lt(12)').removeClass('hidden');
  $('.inner-content').children('.front-pro:gt(11)').addClass('hidden');
  $(".next").show();
  $(".prev").hide();
}
.front-pro.hidden {
  display: none;
}
.prev {
  display: none;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner-content">
  <div class="front-pro">1</div>
  <div class="front-pro">2</div>
  <div class="front-pro">3</div>
  <div class="front-pro">4</div>
  <div class="front-pro">5</div>
  <div class="front-pro">6</div>
  <div class="front-pro">7</div>
  <div class="front-pro">8</div>
  <div class="front-pro">9</div>
  <div class="front-pro">10</div>
  <div class="front-pro">11</div>
  <div class="front-pro">12</div>
  <div class="front-pro hidden">13</div>
  <div class="front-pro hidden">14</div>
</div>
<div class="next" onclick="searchNext();">next</div>
<div class="prev" onclick="searchPrev();">prev</div>

I create a general solution after your ment with next and previous(I use step 3 for demo purposes but you can use what ever you want):

var pager = (function() {
  /*declare private variables*/
  var first = 0,
    last = 2,
    step = 3;

  function searchNext() {
    //next function
    //increasing first and last variables
    first += step;
    last += step;
    pagerHelper();
  }

  function searchPrev() {
    //previous function
    //decrease first and last variables
    first -= step;
    last -= step;
    pagerHelper();
  }

  function pagerHelper() {
    //get all child elements with class front-pro 
    //of element with class .inner-content
    var childElems = $(".inner-content .front-pro");
    //iterate through the elements
    childElems.each(function(i, el) {
      //show the elements that match the criteria removing css class
      if (i >= first && i <= last) {
        $(el).removeClass('hidden');
      } else {
        //hide rest
        $(el).addClass('hidden');
      }
    });
    nextPreviousToggle(childElems.length);
  }

  function nextPreviousToggle(maxEle) {
    //here the code is to show/hide next/previous buttons
    if (last >= maxEle) {
      $(".next").hide();
    } else {
      $(".next").show();
    }
    if (first === 0) {
      $(".prev").hide();
    } else {
      $(".prev").show();
    }
  }
  return {
    searchNext: searchNext,
    searchPrev: searchPrev
  }
})();
.front-pro.hidden {
  display: none;
}
.prev {
  display: none;
}
.prev:hover,
.next:hover {
  text-decoration: underline;
  cursor: pointer;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner-content">
  <div class="front-pro">1</div>
  <div class="front-pro">2</div>
  <div class="front-pro">3</div>
  <div class="front-pro hidden">4</div>
  <div class="front-pro hidden">5</div>
  <div class="front-pro hidden">6</div>
  <div class="front-pro hidden">7</div>
  <div class="front-pro hidden">8</div>
  <div class="front-pro hidden">9</div>
  <div class="front-pro hidden">10</div>
  <div class="front-pro hidden">11</div>
  <div class="front-pro hidden">12</div>
  <div class="front-pro hidden">13</div>
  <div class="front-pro hidden">14</div>
</div>
<span class="next" onclick="pager.searchNext();">next</span>
<span class="prev" onclick="pager.searchPrev();">prev</span>

References

:gt()

:lt()

You use the following code to handle any number of divs,

var x = $(".inner-content div").hide();

$("div:contains(next)").click(function() {
  var cnt = $(this).data("cnt") || 0;
  if((cnt * 12) > x.length){ cnt = 0; }
  x.hide().filter(":eq("+ (cnt * 12)  + "), :lt(" + ((cnt * 12) + 12) + "):gt(" + (cnt * 12) + ")").show();
  $(this).data("cnt", ++cnt);
});

DEMO

Try this instead

$('.inner-content').children().each(function (i, x) {
    if (i < 12) // Hide First 12 i.e 0-11
        $(x).addClass('hidden');
    else if (i < 24) // Show Next 12 i.e 12-23
        $(x).removeClass('hidden');
})

Also make sure you have css rule defined as

.hidden {
    display: none;
}
Post a comment

comment list (0)

  1. No comments so far