Simple HTML DOM - Child Selectors (CSS)

我是研究僧i 提交于 2019-12-23 10:53:05

问题


I am trying to select a (direct) child of parents div.element using the > combinator, but it's failing.

HTML:

<div class="element">
    <p>test</p>
</div>

<div class="element">
    <div class="selected">
        <p>test2</p>
    </div>
</div>

PHP:

$html->find('div.element > p', 0);

I am looking to select the direct p element.

If it's a nested descendant - it shouldn't return anything, but it returns test2.

How can I write it to return test, but not test2? Thanks

UPDATE: The general consensus here on SO seems to be that Simple HTML DOM is bad. I ended up writing my code using PHP's DOMDocument as suggested by Phil. I did test out nevermind's solution and it did work as well. Thanks for all the help and Happy Coding


回答1:


Well, this should (must, actually:)) work (tested on 4 divs):

foreach($html->find('div.element') as $element) {


$paragraph=$element->find('p',0);

    if($paragraph==$element->first_child())
    echo $paragraph;

}


来源:https://stackoverflow.com/questions/31977488/simple-html-dom-child-selectors-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!