我有一个这种形式的大文件[类似的div在整个]。我希望能够选择一个div,找到其中的ul的数量并遍历每个div以获得其中每个li的值。
<div class="experiment">
<div class="experiment-number">5</div>
<ul class="data-values">
<li><div></div> 14</li>
<li><div></div> 15</li>
</ul>
<ul class="data-values">
<li><div></div> 16</li>
</ul>
</div>
想用phpQuery做。已经在这里使用jQuery解决了: How to find number of <ul> inside each div
答案 0 :(得分:0)
我不知道PHPQuery,但是在StackOverflow上使用它的doc,你的jQuery代码和一些已经存在的phpQuery问题,这个应该可以工作。
$experiments = pq('.experiment');
foreach($experiments as $experiment){
echo "Experiment: ". $experiment->find('.experiment-number')->text();
echo "Experiment ULs: " + $experiment->find('ul')->size();
$total = 0;
foreach($experiment->find('ul.data-values li') as $t){
$total += (int) $t;
}
echo "Experiment total: " . $total;
}
编辑(说真的,你应该看到3个错误):
$experiments = pq('.experiment');
foreach($experiments as $experiment){
$experiment = pq($experiment);
echo "Experiment: ". $experiment->find('.experiment-number')->text();
echo "Experiment ULs: " . $experiment->find('ul')->size();
$total = 0;
foreach($experiment->find('ul.data-values li') as $t){
$t = pq($t);
$total += (int) $t->text();
}
echo "Experiment total: " . $total;
}
+
成为.
(我复制你的jQuery代码的一个错误)(int) $t->text();