后退反向数字循环

时间:2017-11-21 11:04:54

标签: javascript jquery

我正在尝试使用jquery创建一个小的分页系统。以下代码已生成正向页码,但我必须对向后页数执行相同操作。请阅读下面的代码。我在代码中评论过。问你可能有的任何问题。提前致谢

<!DOCTYPE html>
<html>
<head>
    <title>test</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</head>
<body>


<ul class="pagination"></ul>

<script type="text/javascript">
function paginate(pagestart, pageNumber) {
                for (var i = pagestart; i < (pagestart + pageNumber); i++) {
                    $(".pagination").append("<li><a href=\"index?page=" + i + "\">" + i + "</a></li>");
                    if(i > pageNumber){
                        //when "pageNumber" more then "i" it will display reverse back page number until hit -> "pageNumber"
                    }
                }
            }

            paginate(1, 5);


</script>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

追加=&gt;前置解决方案较低

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 > pageNumber) {
      //when "pageNumber" more then "i" it will display reverse back page number until hit -> "pageNumber"
    }
  }
}

paginate(1, 5);

function Rpaginate(pagestart, pageNumber) {
  for (var i = pagestart; i < (pagestart + pageNumber); i++) {
    $(".pagination.rev").prepend("<li><a href=\"index?page=" + i + "\">" + i + "</a></li>");
  }
}

Rpaginate(1, 5);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<ul class="pagination sol"></ul>
<br>
<ul class="pagination rev"></ul>