有問題嗎,點(diǎn)第一下沒反應(yīng)。
<script?type="text/javascript"> function?clearText()?{ ??var?content=document.getElementById("content"); ??//?在此完成該函數(shù) ??var?x=content.childNodes; ?? ??for(var?i=0;i<x.length;i++){ ??????var?y=content.removeChild(x[i]); ??????y=null; ??} ?? } </script>
點(diǎn)第二下 ?1,2 ,5節(jié)點(diǎn)清除了,咋回事
2016-03-06
因?yàn)閏hildnodes集合是動(dòng)態(tài)改變的,在for循環(huán)中i的值不斷遞增,前面的屬性就刪不掉了。
x在第一次循環(huán)的時(shí)候是這樣的[text, h1, text, h1, text, h1, text, h1, text, h1, text]
i=0 ,text刪掉,此時(shí)x變成[ h1, text, h1, text, h1, text, h1, text, h1, text]
i=1,此時(shí)X變成[ h1, h1, text, h1, text, h1, text, h1, text]
....
應(yīng)該明白了吧。為什么要點(diǎn)兩次才會(huì)刪除h