【发布时间】:2017-05-05 18:21:05
【问题描述】:
如何从最后按顺序获取元素?
例如,我需要从最后获取第二个项目。
<?php
$html = <<<EOD
<div style="text-align: center;" class="navigation">
<span class="pagination">
<span>« novější</span> |
<span>1</span> |
<a href="?listType=&sort=&page=1">2</a> |
<a href="?listType=&sort=&page=2">3</a> |
<a href="?listType=&sort=&page=3">4</a> |
<a href="?listType=&sort=&page=1">starší »</a>
</span>
</div>
EOD;
$dom = PHPQuery::newDocument($html);
var_dump($dom->find('.navigation > .pagination > *')->eq(-2)->text());
var_dump($dom->find('.navigation > .pagination > *:last-child(2)')->text());
但此代码返回的不是 4:
string(0) ""
string(11) "starší »"
【问题讨论】:
-
你想要这个
starší作为输出吗? -
为什么不: $dom->find('.navigation > .pagination > *:eq(-2)')->text() ? last-child(2) 是否在 PHPQuery 中接受参数,因为在 jQuery 中它不接受并且只返回最后一个孩子,在你的情况下是“starší »”。