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