就像标题所说的那样,我的php removeChild函数有问题
我有这个脚本遍历html标签
$list = $dom->getElementsByTagName("tag");
//charge et supprimme les tags
while($lst = $list->item(0)) {
$tgs = ucfirst($lst->getAttribute("type"));
array_push($test, $tgs);
$tg = new $tgs($dom, $lst, false);
$tg->render();
var_dump("--------- 1 ---------");
for ($i=0; $i < $list->count(); $i++) {
var_dump($list->item($i)->getAttribute("type"));
}
// var_dump($list[0]->parentNode->nodeName);
$lst->parentNode->removeChild($lst); //this line
var_dump("--------- 2 ---------");
for ($i=0; $i < $list->count(); $i++) {
var_dump($list->item($i)->getAttribute("type"));
}
$list = $dom->getElementsByTagName("tag");
}
和此html文件
<!DOCTYPE html>
<html lang="fr">
<head>
</head>
<body>
<header>
<tag type="includes" file="header" />
</header>
<main class="test container">
<div class="bloc_principal">
<div class="cover-black">
<div class="bloc_principal_titre">
<h1>L'actualité de nos campus...</h1>
</div>
<div class="test2">
<h2 id="titre-home">Ceci est un magnifique titre</h2>
<p class="p-home">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<div class="bloc-plus">
En savoir plus
</div>
</div>
</div>
</div>
</div>
<h3 id="titre-between">Articles récents</h3>
<div class="row articles">
<tag type="loop" for="posts" limit="6">
<div class="col-12 col-md-6 col-lg-4">
<div class="article column-categorie">
<h4 class="title">
<loop column="title" />
</h4>
<div class="etiquettes">
<span class="cat">
<loop column="categorie" />
</span>
<span class="date">
<loop column="date" />
</span>
</div>
<div class="text">
<loop column="content" />
</p>
</div>
</div>
</tag>
</div>
</main>
<footer>
<tag type="includes" file="footer" />
</footer>
</body>
</html>
大多数都能正常工作。
但是由于某种原因,函数$lst->parentNode->removeChild($lst);
删除了
<tag type="loop">
<footer><tag type="includes"></footer>
当我只应删除<tag type="loop">
当我var_dump $ lst-> parentNode时,我获得了删除标记(<div class="row articles">
)的正确父对象
答案 0 :(得分:0)
好吧,我发现html的格式错误,并且</div>
没放在正确的行,所以我的最后一个<tag type="includes">
被先例删除了