[英]php the foreach loop applied to DomNodeList skips an element when appending
我正在使用php從頁面加載元素。 它幾乎可以工作。 唯一的事情是,從所有錨定元素中我嘗試加載它只會給我一半。 它跳過第二個元素。 這是我的一些PHP腳本
$div = @$doc->getElementById('topicList');
$anchs=$div->getElementsByTagName('a');
//echo $anchs->length; it does have the correct length
$container = $doc->createElement("div");
$container->setAttribute('class', 'relative');
foreach ($anchs as $anch){
$container->appendChild($anch);
}
/// /////////
$expDiv = $doc->createElement("div");
$expDiv->setAttribute('class', 'explanation_div');
$container->appendChild($expDiv);
echo utf8_decode(@$doc->saveXML($container));
因此,這僅釋放出第二個錨元素,而不是全部。
我已經用for循環嘗試過了,但是在DomNodeList上是不允許的。
我認為它必須與appendChild有關,並且可能會將下一個追加到上一個或之后的東西,但是我不知道如何做。
以前有人遇到過這個問題嗎,或者您能看到我做錯了嗎? 非常感謝幫助!
我現在解決了。 事實證明, appendChild
方法會彈出列表的項目,因此所有元素都移回一個位置。 這工作:
for ( $i=0;$i<$anchs->length;){ // so don't increase $i
$anch=$anchs->item($i);
$container->appendChild($anch);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.