如何使用phpQuery获取下一页网址?

时间:2018-11-11 12:41:57

标签: php jquery phpquery

我有自定义HTML分页模板:

<ul class="number-list">       
    <li><a href="/article/" class="page-number js-page-filter " data-page="1">1</a></li>
    // Current page with class [.red]
    <li><a href="/article/?page=2" class="page-number js-page-filter red" data-page="2">2</a></li>
    <li><a href="/article/?page=3" class="page-number js-page-filter " data-page="3">3</a></li>
    <li><a href="/article/?page=4" class="page-number js-page-filter " data-page="4">4</a></li>
    <li><a href="/article/?page=5" class="page-number js-page-filter " data-page="5">5</a></li>
    <li><span class="page-number">...</span></li>
    <li><a href="/article/?page=31" class="page-number js-page-filter " data-page="31">31</a></li>
    <li><a href="/article/?page=32" class="page-number js-page-filter " data-page="32">32</a></li>
</ul>

我可以使用以下代码获取当前页面的网址:

$current = $doc->find('.number-list li a.red')->attr('href');

如何获取我的下一页网址?

我很累:

$next = $doc->find('.number-list li a.red')->next('a')->attr('href');
$next = $doc->find('.number-list li a.red')->next('li a')->attr('href');
$next = $doc->find('.number-list li a.red')->next('.page-number')->attr('href');
$next = $doc->find('li a.red')->next('li a')->attr('href');
$next = $doc->find('li a.red')->next('li .page-number')->attr('href');

1 个答案:

答案 0 :(得分:1)

安德烈亚斯。您可以获取所有<li>并检查其<a>是否具有类red。 该元素将是您当前的元素,之后,您可以获取下一个元素。 希望对您有帮助。

$document = phpQuery::newDocument('
<ul class="number-list">       
    <li><a href="/article/" class="page-number js-page-filter " data-page="1">1</a></li>
    <li><a href="/article/?page=2" class="page-number js-page-filter red" data-page="2">2</a></li>
    <li><a href="/article/?page=3" class="page-number js-page-filter " data-page="3">3</a></li>
    <li><a href="/article/?page=4" class="page-number js-page-filter " data-page="4">4</a></li>
    <li><a href="/article/?page=5" class="page-number js-page-filter " data-page="5">5</a></li>
    <li><span class="page-number">...</span></li>
    <li><a href="/article/?page=31" class="page-number js-page-filter " data-page="31">31</a></li>
    <li><a href="/article/?page=32" class="page-number js-page-filter " data-page="32">32</a></li>
</ul>
');

$lis = $document->find('.number-list li');
foreach ($lis as $li)
{
    if(pq($li)->find('a')->hasClass('red'))
    {
       var_dump(pq($li)->next()->find('a')->attr('href')); // get the next li and it a tag
    }
}