I ve got a follow up question to my question that has been answered by Jack: Wrap segments of HTML with divs (and generate table of contents from HTML-tags) with PHP
我一直在试图给上述答案添加一些功能,以便取得以下结果。
这是我现在的HTML:
<h3>Subtitle</h3>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<h3>Another subtile
<h3>
<p>Yet another paragraph</p>
这就是我想要实现的目标:
<h3 class="current">Subtitle</h3>
<div class="ac_pane" style="display:block;">
<p>This is a paragraph</p>
<p>This is another paragraph</p>
</div>
<h3>Another subtitle</h3>
<div class="ac_pane">
<p>Yet another paragraph</p>
</div>
我一直试图修改代码 出上面的例子, 但无法理解它:
foreach ($d->getElementsByTagName( h3 ) as $h3) {
$ac_pane_nodes = array($h3);
for ($next = $h3->nextSibling; $next && $next->nodeName != h3 ; $next = $next->nextSibling) {
$ac_pane_nodes[] = $next;
}
$ac_pane = $d->createElement( div );
$ac_pane->setAttribute( class , ac_pane );
// Here I m trying to wrap all tags between h3-sets, but am failing!
$h3->parentNode->appendChild($ac_pane, $h3);
foreach ($ac_pane_nodes as $node) {
$ac_pane->appendChild($node);
}
}
请注意,在第一个h3集中添加 class="目前"
, 并在第一个 div.ac_pane
中添加 system="display:block;"
是可选的,但非常感激。