问题
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