我有自定义的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>
我可以使用以下代码获取当前页面的url:
$current = $doc->find('.number-list li a.red')->attr('href');
在我的情况下,如何获取下一页url?
我累了:
$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');
Andreas。您可以获取所有的<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
}
}