0

我正在研究一个 php ajax 分页。

问题是它不会正确地宣传 class="highlightActivePage" 。

如果我点击 1 - 2 - 3 ,它可以正常工作并突出显示数字 3 如果我在第 3 页。

但是当我点击第 4 页时,它会突出显示第 5 页,等等。

不确定我的 for 循环是否错误,但这里是我的代码:

if (paginationHTML == "")
        {

            paginationHTML += "<ul>";

            if (adResultsData.show_first_text == 1)
            {
                paginationHTML += "<li><a href='#' onclick='fetchResults(1);'>First</a></li>";  
            }

            if (adResultsData.show_previous_text == 1)
            {
                paginationHTML += "<li><a href='#' onclick='fetchResults(" + (adResultsData.current_page - 1) + ");'>Prev</a></li>";    
            }

            for (var i = 0; i < adResultsData.pages.length; i++)
            {
                if (adResultsData.current_page == (i + 1))
                {
                    paginationHTML += "<li><a href='#' class='highlightActivePage' onclick='fetchResults(" + adResultsData.pages[i] + ");'>" + adResultsData.pages[i] + "</a></li>";
                }
                else
                {
                    paginationHTML += "<li><a href='#' onclick='fetchResults(" + adResultsData.pages[i] + ");'>" + adResultsData.pages[i] + "</a></li>";
                }

            }

            if (adResultsData.show_next_text == 1)
            {
                paginationHTML += "<li><a href='#' onclick='fetchResults(" + (adResultsData.current_page + 1) + ");'>Next</a></li>";    
            }

            if (adResultsData.show_last_text == 1)
            {
                paginationHTML += "<li><a href='#' onclick='fetchResults(" + adResultsData.number_of_pages + ");'>last</a></li>";   
            }

            paginationHTML += "</ul>";
            paginationHTML += pageSpan.innerHTML = "<br>Page " + adResultsData.current_page + " of " + adResultsData.number_of_pages; 
        }

php

    $numberOfPages = $results['pages'];
$currentPage = $results['currentPage'];



if ($currentPage != 1 && $currentPage != 2)
{$showFirst = 1;}
else $showFirst = 0;

if ($currentPage != 1)
{$showPrevious = 1;}
else $showPrevious = 0;

if ($currentPage != $numberOfPages)
{$showNext = 1;}
else $showNext = 0;

if ($currentPage != $numberOfPages && $currentPage != ($numberOfPages - 1))
{$showLast = 1;}
else $showLast = 0;



if ($currentPage <= 5 && $numberOfPages <= 5 || $numberOfPages <= 5)
{$startingPage = 1;}
else if ($currentPage == 1 || $currentPage == 2)
{$startingPage = 1;}
else
{$startingPage = $currentPage - 2;}



$pageNumbers = [];
for ($i = $startingPage; $i < ($startingPage + 5) && $i <= $numberOfPages; $i++)
{
    $pageNumbers[] = $i;
}

$pagesString = implode(", ", $pageNumbers); $listingsString = implode(", ", $listingsArray);

$jsonString = <<< END { "resultsTotal" : $numberOfResults, "listings" : [$listingsString], "number_of_pages" : $numberOfPages, "current_page" : $currentPage,

谢谢!

4

0 回答 0