我想在 HTML 元素中获取注释,但显然,注释不在 DOM 元素的层次结构中。
例如,
<!-- FIRST -->
<div id="parent">
<!-- SECOND -->
<span id="child">
<!-- THIRD -->
</span
</div>
使用的第二个参数时$xpath->query无效。
$comments=$xpath->query('//comment()');
foreach($comments as $comment){
echo $comment->nodeValue.PHP_EOL;
}
// Query (intended) within <div id="parent">
$el = $doc->getElementById('parent');
$comments=$xpath->query('//comment()',$el);
foreach($comments as $comment){
echo $comment->nodeValue.PHP_EOL;
}
// Query (intended) within <div id="child">
$el = $doc->getElementById('child');
$comments=$xpath->query('//comment()',$el);
foreach($comments as $comment){
echo $comment->nodeValue.PHP_EOL;
}
并且所有三个循环的输出都是相同的,
FIRST
SECOND
THIRD
FIRST
SECOND
THIRD
FIRST
SECOND
THIRD
largeQ