請大家?guī)兔纯催@段代碼問題出在哪里
for循環(huán)里循環(huán)條件為i<=4,無法實現(xiàn)一次清除所有節(jié)點,這樣寫的效果是先清除前2個節(jié)點,再點按鈕才能清除所有節(jié)點。請大家?guī)兔纯磫栴}出在哪里?
<script type="text/javascript">
function clearText() {
? var content=document.getElementById("content");
? // 在此完成該函數(shù)
? for(var i = 0;i < 4;i++){
? ? content.removeChild(content.childNodes[0]);
? }
}
2018-05-24
有空白節(jié)點,for循環(huán)的條件應(yīng)該是? i<content.childNodes.length
2018-05-24
function?clearText()?{ ????var?mystr=document.getElementById("content"); ????for(var?i=mystr.childNodes.length-1;i>=0;i--){ ????????mystr.removeChild(mystr.childNodes[i]); ????} }