有問題嗎,點第一下沒反應(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>
點第二下 ?1,2 ,5節(jié)點清除了,咋回事
2016-03-06
因為childnodes集合是動態(tài)改變的,在for循環(huán)中i的值不斷遞增,前面的屬性就刪不掉了。
x在第一次循環(huán)的時候是這樣的[text, h1, text, h1, text, h1, text, h1, text, h1, text]
i=0 ,text刪掉,此時x變成[ h1, text, h1, text, h1, text, h1, text, h1, text]
i=1,此時X變成[ h1, h1, text, h1, text, h1, text, h1, text]
....
應(yīng)該明白了吧。為什么要點兩次才會刪除h