为什么可以一起性删除?我看了置顶的解答但是还是有些不懂。。。。。
<!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(i=0;i<content.childNodes.length;i++){ if(content.childNodes[i].nodeType==1){ content.removeChild(content.childNodes[i]); } } } </script> <button onclick="clearText()">清除节点内容</button> </body> </html> function clearText() { var content=document.getElementById("content"); for(var i=0;i<content.childNodes.length;i++){ if(content.childNodes[i].nodeType!=1){ continue; }else{ content.removeChild(content.childNodes[i]); } } }
第一段代码的问题:为什么可以一起性删除?i<content.childNodes.length这里比较的也是变值,if判断每当是元素时删除一个元素,当再次判断时候content.childNodes.length的值已经少了1,再判断就是1<4,删除元素,继续执行、、2<3删除元素,继续执行、..当3<2就应该删除不了啦?
第二段代码的问题:continue; 在这里起到什么作用?是不是当元素类型不等于1执行continue?