使用phpQuery选择第一个类型为text的DOM元素

时间:2011-12-23 23:33:02

标签: php jquery phpquery

假设我有这段代码,

<div id="id1">
  This is some text
  <div class="class1"><p>lala</p> Some markup</div>
</div>

我想要的只是没有子元素.class1内容的文本“这是一些文本”。我可以使用$('#id1').contents().eq(0).text()在jquery中完成它,我如何在phpQuery中执行此操作?

感谢。

2 个答案:

答案 0 :(得分:2)

我的坏,我正在做

pq('#id1.contents().eq(0).text()')

而不是

pq('#id1')->contents()->eq(0)->text()

答案 1 :(得分:0)

如果您正在追求兼容性,并且想要将元素遍历/操作为DOM对象,那么您可能正在使用PHP DOM XML库:http://www.php.net/manual/en/book.domxml.php

您的代码看起来像这样:

$xml = xmldoc('<div id="id1">This is some text<div class="class1"><p>lala</p> Some markup</div></div>');
$node = $xml->get_element_by_id("id1");
$content = $node->get_content(); 

对不起,我现在没有时间对此进行测试,但希望它能让你朝着正确的方向前进,并形成一个体面修改的基础......有一个很好的清单但是PHP文档中的DOM遍历函数:)

参考文献:http://www.php.net/manual/en/book.domxml.phphttp://www.php.net/manual/en/function.domdocument-get-element-by-id.phphttp://www.php.net/manual/en/function.domnode-get-content.php