條件循環(huán)錯在哪里了
<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++){
? var x=content.removeChild(content.childNodes[i]);}/
??
? return x;
}
</script>
<button onclick="clearText()">清除節(jié)點(diǎn)內(nèi)容</button>
</body>
</html>
2016-08-08
這樣子一次性清理不完,是因?yàn)槊看吻謇硪粋€節(jié)點(diǎn)后,后面的節(jié)點(diǎn)會頂上來,造成一般一般的清理。要么不要i++。每次都清除第一個節(jié)點(diǎn),要么使用i--。
我的寫法:
function clearText() {
? var content=document.getElementById("content");
? // 在此完成該函數(shù)
? var num = content.childNodes.length;
? for (var i=num-1;i>0;i--){
? ? ? var node = content.childNodes[i];
? ? ? content.removeChild(node);
? }
}
2016-08-06
for循環(huán)里面的長度換一個變量去裝,不要放在那里面,否則會出錯