<!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");? ? var conCl=content.childNodes;? ? if(conCl[0]!=1)? ? {? ? ? ? content.removeChild(conCl[0]);? ? }? ? var x=content.removeChild(conCl[0]);? ? var newh1=document.createElement("h1"); ?? ? newh1.innerHTML=x.innerHTML; ?? ? content.appendChild(newh1);}</script><button onclick="clearText()">清除節(jié)點內(nèi)容</button></body></html>
下面的代碼為什么不會無限循環(huán)下去呢?我刪除了一個子元素也添加了一個子元素,應(yīng)該是無限循環(huán)的吧?
徐錦杰
2016-11-06 19:46:06