我正在使用 xpath 表达式来确定我的 DOM 树中的某个 div 类(感谢 VolkerK!)。
foreach($xpath->query('//div[@class="posts" and div[@class="foo"]]') as $node)
$html['content'] = $node->textContent;
//$html['node-position'] = $node->position(); // (global) index position of the child 'foo'
}
最终我需要知道我的孩子 'foo' 有哪个(全局)索引位置,因为我想稍后用 jQuery 替换它:eq()或 nth-child()。
有没有办法做到这一点?
我正在跟进我的另一个关于选择正确元素的问题(XPath/Domdocument check for child by class name)。
谢谢!
更新:
我发现使用:
$html['node-position'] = $node->getNodePath()
实际上给了我父节点的xpath语法(/html/body/div[3])的路径和元素编号,但是对于子div'foo'怎么办?