為什么循環(huán)設(shè)置為 for(i=0;i<=content.childNodes.length;i++)時(shí)。要點(diǎn)三次才能清除?
為什么循環(huán)設(shè)置為 for(i=0;i<=content.childNodes.length;i++)時(shí)。要點(diǎn)三次才能清除?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</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");
? // 在此完成該函數(shù)
? for(i=0;i<content.childNodes.length;i++){
? ? ?var childNode = content.childNodes[i];
? ? ?content.removeChild(childNode);
? }
}
</script>
<button onclick="clearText()">清除節(jié)點(diǎn)內(nèi)容</button>
</body>
</html>
2016-04-08
因?yàn)槭莿h除操作 你每執(zhí)行一次循環(huán)? 你的長(zhǎng)度就少一分。。。你的循環(huán)體設(shè)置的不合理。
2015-07-14
content.childNodes.length是多少???看看值是多少,從0開始,循環(huán)幾次。