<!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");
??//?在此完成該函數(shù)
??
??//法一,一次性清除節(jié)點內容
????for(var?i=0;i<content.childNodes.length;i++){
??????if(content.childNodes[i].nodeType!=1){???
?????????continue;??
??????}else{
?????????content.removeChild(content.childNodes[i]);????
??????}
?????????
??//法二,多次方清除節(jié)點內容
/*
???for(var?i=0;i<content.childNodes.length;i++){
????var?chnode=content.childNodes[i];
????content.removeChild(chnode);??
??}
*/
??
??}
??
</script>
<button?onclick="clearText()">清除節(jié)點內容</button>
</body>
</html>法一跟法二的兩個for循環(huán)有什么本質的區(qū)別 ? 為什么第一個for循環(huán)能直接刪除所有內容 而第二個for循環(huán)只能多次刪除內容? 一直想不通
求大神幫看一下 關于for循環(huán)的疑問?
csusun
2016-03-19 12:14:54