【问题标题】:How to chain in phpquery (almost everything can be a chain)如何在phpquery中链(几乎所有东西都可以是链)
【发布时间】:2014-01-17 10:13:08
【问题描述】:

大家好, 我对 phpquery 很陌生,这是我在 stackoverflow 上的第一篇文章,原因是我找不到正确的 phpquery 链接语法。我知道有人知道我一直在寻找什么。

我只想删除 div 中的某个 div。

 <div id = "content"> 
        <p>The text that i want to display</p>
        <div class="node-links">Stuff i want to remove</div>
 </content>

这几行代码完美运行

 pq('div.node-links')->remove();
 $text = pq('div#content');
 print $text; //output: The text that i want to display

但是当我尝试时

$text = pq('div#content')->removeClass('div.node-links'); //or
$text = pq('div#content')->remove('div.node-links');

 //output: The text that i want to display (+) Stuff i want to remove

谁能告诉我为什么第二个代码块不起作用?

谢谢!

【问题讨论】:

    标签: php jquery phpquery


    【解决方案1】:

    第一行代码仅在您尝试从div.node-links 中删除类时才有效,它不会删除节点。

    如果您尝试删除需要更改的类:

    $text = pq('div#content')->removeClass('div.node-links');
    // to
    $text = pq('div#content')->find('.node-links')->removeClass('node-links')->end();
    

    将输出:

    <div id="content">
        <p>The text that i want to display</p>
        <div>Stuff i want to remove</div>
    </div>
    

    至于第二行代码.. 我不确定为什么它不起作用,您似乎没有选择 .node-links 但我能够使用这些获得所需的结果。

    // $markup = file_get_contents('test.html');
    // $doc = phpQuery::newDocumentHTML($markup);
    $text = $doc->find('div#content')->children()->remove('.node-links')->end();
    
    // or 
    
    $text = pq('div#content')->find('.node-links')->remove()->end();
    
    // or 
    
    $text = pq('div#content > *')->remove('.node-links')->parent();
    

    希望对你有帮助

    【讨论】:

    • 谢谢r-sal先生,好像第二个正在工作 $text = pq('div#content')->find('.node-links')->remove() ->结束();我已经找到了另一种方法,因为元素存储在数据库中,所以我添加了两个字段,用于 fullContent 的字段和用于需要删除()的元素的字段。我正在为多个 RSS 提要开发一个动态刮板(我现在有 200 个提要 URL)。我喜欢 phpquery 让抓取更简单,但唯一的问题是他们的文档。无论如何,非常感谢您的回答,干杯!
    【解决方案2】:

    由于remove()不带任何参数,你可以这样做:

    $text = pq('div#content div.node-links')->remove();
    

    【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多