如何封装一个高效的原生JavaScript分页函数?

更新于
2026-09-24 22:13:24
1阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计360个文字,预计阅读时间需要2分钟。

如何封装一个高效的原生JavaScript分页函数?

python/** * 分页函数 * @param $nowPage int 当前页 * @param $totalPage int 总页数 * @param $url string 跳转的连接 * @example http://www.home.com/class/day2/code/page.php?xxx=xxxxxx=xxxpage */function pageHtml($nowPage, $totalPage, $url) { // 生成分页HTML}

gistfile1.txt

/** * 分页函数 * @param $nowPage int 当前页 * @param $totalPage int 总页数 * @param $url string 跳转的连接,例:www.home.com/class/day2/code/page.php?xxx=xxx&xxx=xxx&page */ function pageHtml($nowPage, $totalPage, $url){ #构建左半边部分 //左半边需要的参数 $preOnePage = $nowPage-1;//当前页的上一页 $preTwoPage = $nowPage-2;//当前页的上上页 if( $nowPage==1 ){//当前页为左边界 $leftHtml = ""; }elseif( $preOnePage==1 ) {//当前页的上一页为左边界 $leftHtml = "

  • 上一页
  • "; $leftHtml .= "
  • $preOnePage
  • "; }elseif( $preTwoPage==1 ) {//当前页的上上页为左边界 $leftHtml = "
  • 上一页
  • "; $leftHtml .= "
  • $preTwoPage
  • "; $leftHtml .= "
  • $preOnePage
  • "; }else{//其他情况 $leftHtml = "
  • 上一页
  • "; $leftHtml .= "... "; $leftHtml .= "
  • $preTwoPage
  • "; $leftHtml .= "
  • $preOnePage
  • "; } #构建当前页部分 $nowHtml = "
  • $nowPage
  • "; #构建右半边的部分 //右半边需要的参数 $nextOnePage = $nowPage+1;//当前页的下一页 $nextTwoPage = $nowPage+2;//当前页的下下页 if( $nowPage==$totalPage ){//当前页为右边界 $rightHtml = ""; }elseif( $nextOnePage==$totalPage ) {//当前页的下一页为右边界 $rightHtml = "
  • $nextOnePage
  • "; $rightHtml .= "
  • 下一页
  • "; }elseif( $nextTwoPage==$totalPage ) {//当前页的下下页为右边界 $rightHtml = "
  • $nextOnePage
  • "; $rightHtml .= "
  • $nextTwoPage
  • "; $rightHtml .= "
  • 下一页
  • "; }else{//其他情况 $rightHtml = "
  • $nextOnePage
  • "; $rightHtml .= "
  • $nextTwoPage
  • "; $rightHtml .= "... "; $rightHtml .= "
  • 下一页
  • "; } //拼接分页HTML $pageHtml = $leftHtml . $nowHtml . $rightHtml; return $pageHtml; }

    如何封装一个高效的原生JavaScript分页函数?

    本文共计360个文字,预计阅读时间需要2分钟。

    如何封装一个高效的原生JavaScript分页函数?

    python/** * 分页函数 * @param $nowPage int 当前页 * @param $totalPage int 总页数 * @param $url string 跳转的连接 * @example http://www.home.com/class/day2/code/page.php?xxx=xxxxxx=xxxpage */function pageHtml($nowPage, $totalPage, $url) { // 生成分页HTML}

    gistfile1.txt

    /** * 分页函数 * @param $nowPage int 当前页 * @param $totalPage int 总页数 * @param $url string 跳转的连接,例:www.home.com/class/day2/code/page.php?xxx=xxx&xxx=xxx&page */ function pageHtml($nowPage, $totalPage, $url){ #构建左半边部分 //左半边需要的参数 $preOnePage = $nowPage-1;//当前页的上一页 $preTwoPage = $nowPage-2;//当前页的上上页 if( $nowPage==1 ){//当前页为左边界 $leftHtml = ""; }elseif( $preOnePage==1 ) {//当前页的上一页为左边界 $leftHtml = "

  • 上一页
  • "; $leftHtml .= "
  • $preOnePage
  • "; }elseif( $preTwoPage==1 ) {//当前页的上上页为左边界 $leftHtml = "
  • 上一页
  • "; $leftHtml .= "
  • $preTwoPage
  • "; $leftHtml .= "
  • $preOnePage
  • "; }else{//其他情况 $leftHtml = "
  • 上一页
  • "; $leftHtml .= "... "; $leftHtml .= "
  • $preTwoPage
  • "; $leftHtml .= "
  • $preOnePage
  • "; } #构建当前页部分 $nowHtml = "
  • $nowPage
  • "; #构建右半边的部分 //右半边需要的参数 $nextOnePage = $nowPage+1;//当前页的下一页 $nextTwoPage = $nowPage+2;//当前页的下下页 if( $nowPage==$totalPage ){//当前页为右边界 $rightHtml = ""; }elseif( $nextOnePage==$totalPage ) {//当前页的下一页为右边界 $rightHtml = "
  • $nextOnePage
  • "; $rightHtml .= "
  • 下一页
  • "; }elseif( $nextTwoPage==$totalPage ) {//当前页的下下页为右边界 $rightHtml = "
  • $nextOnePage
  • "; $rightHtml .= "
  • $nextTwoPage
  • "; $rightHtml .= "
  • 下一页
  • "; }else{//其他情况 $rightHtml = "
  • $nextOnePage
  • "; $rightHtml .= "
  • $nextTwoPage
  • "; $rightHtml .= "... "; $rightHtml .= "
  • 下一页
  • "; } //拼接分页HTML $pageHtml = $leftHtml . $nowHtml . $rightHtml; return $pageHtml; }

    如何封装一个高效的原生JavaScript分页函数?