分页以前的链接生成

时间:2017-11-21 13:25:43

标签: javascript jquery

检查以下代码。我的目标是:当“pagestart”输入等于或大于5时,页面将显示先前的分页数。如果没有,该页面将只保留5个页面。我附上了HTML以便更好地理解。提前谢谢。

function paginate(pagestart, pageNumber) {
  for (var i = pagestart; i < (pagestart + pageNumber); i++) {
    $(".pagination.sol").append("<li><a href=\"index?page=" + i + "\">" + i + "</a></li>");
    if (i >= 5) {
      $(".pagination.rev").append("<li><a href=\"index?page=" + i + "\">" + i + "</a></li>");

    }
  }
}
paginate(6, 5);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


<h1>Current code working like this way</h1>
<ul class="pagination rev"></ul>
<ul class="pagination sol"></ul>
<h1>My goal to work like this way when "pagestart" greather or equal to 5</h1>
<ul class="pagination">
  <li><a href="index?page=1">1</a></li>
  <li><a href="index?page=2">2</a></li>
  <li><a href="index?page=3">3</a></li>
  <li><a href="index?page=4">4</a></li>
  <li><a href="index?page=5">5</a></li>
  <li><a href="index?page=6">6</a></li>
  <li><a href="index?page=7">7</a></li>
  <li><a href="index?page=8">8</a></li>
  <li><a href="index?page=9">9</a></li>
  <li><a href="index?page=10">10</a></li>
</ul>
<h1>but if less then 5 then it will display like this</h1>
<ul class="pagination">
  <li><a href="index?page=1">1</a></li>
  <li><a href="index?page=2">2</a></li>
  <li><a href="index?page=3">3</a></li>
  <li><a href="index?page=4">4</a></li>
  <li><a href="index?page=5">5</a></li>
</ul>

1 个答案:

答案 0 :(得分:1)

你可以这样做。

function paginate(id, pagestart, pageNumber) {
  let start = pagestart >=5 ? 1 : pagestart;
  for (let i = start; i < (pagestart + pageNumber); i++) {
    $(id).append('<li><a href="index?page=' + i + '">' + i + '</a></li>');
  }
}
paginate('.pagination.first', 6, 5);
paginate('.pagination.second', 1, 5);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


<p>when "pagestart" i greater or equal to 5</p>
<ul class="pagination first"></ul>

<hr>

<p>When "pagestart" is equal to 5</p>
<ul class="pagination second"></ul>