我想做的是隐藏前12个元素,并显示下一个12个元素。

//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>

这是我的javascript / jquery:
function SearchNext(){
 var first = $('.inner-content').children('.front-pro:hidden:first');
    first.prevAll(':lt(12)').hide();
    first.nextAll(':lt(12)').show();
}

停止工作后,它可以工作一次。 (并跳过数字13)
我要每个下一个单击并隐藏上一个显示12个新元素。

更新-这是我能完美工作的最终结果
JSFIDDLE DEMO

感谢Alex Char

用于创建页码的PHP ,您也可以使用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; }

确保前12个div不包含“hidden”类,之后的所有div都应在该类中具有“hidden”

最佳答案

我对实现和以前的实现做了一些改动。我使用CSS类来隐藏内容。

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.com/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>


在您对下一个和上一个发表评论后,我会创建一个通用的解决方案(我将第3步用于演示目的,但您可以使用所需的任何东西):

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.com/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>


引用

:gt()

:lt()

关于javascript - jQuery隐藏前12个元素,显示下12个元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36289573/

10-10 04:50