removeChild()不能一次性清除
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>無標題文檔</title> </head> <body> <div?id="content"> ??<h1>html</h1> ??<h1>php</h1> ??<h1>javascript</h1> ??<h1>jquery</h1> ??<h1>java</h1> </div> <script?type="text/javascript"> function?clearText()?{ ????var?content=document.getElementById("content"); ????//?在此完成該函數 ????for(var?i?=?0;?i?<?content.childNodes.length;?i++){ ????????content.removeChild(content.childNodes[i]); ????} } </script> <button?onclick="clearText()">清除節點內容</button> </body> </html>
為何我這樣寫不能一次性清除節點內容呢
2016-06-14
問答板塊里瀏覽最多的問答可以幫你解決。
http://www.xianlaiwan.cn/qadetail/56371
2016-07-10
你這里有很多問題!1:content.childNodes.length這個長度在每循環一次都會改變,這也會導致你的i比content.childNodes.length大,所以刪除不完。2:content.removeChild(content.childNodes[i]);干嘛要移除第i個呢?因為第i個不一定存在用你的方法,content.removeChild(content.childNodes[0]);這樣也可以實現。