關(guān)于顯示已刪除的 if語句的條件
<!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?childs=content.childNodes??while?((childs.length>0)&&(childs[0].nodeType!=1))?{????content.removeChild(childs[0])??}??//刪除節(jié)點(diǎn)??var?newDiv=document.createElement("div")??if?(childs.length>0)?{????var?removeNode=content.removeChild(childs[0])????newDiv.innerHTML="clear?"+removeNode.innerHTML+"?done"??}?else?{????newDiv.innerHTML="沒有可以刪除的節(jié)點(diǎn)了"??}??content.parentNode.appendChild(newDiv)??//逐個(gè)顯示已刪除完成}</script><button?onclick="clearText()">清除節(jié)點(diǎn)內(nèi)容</button></body></html>
這是學(xué)生熱門代碼的第二個(gè)。
顯示刪除已完成的if語句的條件:? if (childs.length>0)?
為什么不能寫if?((childs.length>0)&&(childs[0].nodeType!=1))
寫成這樣刪除不了節(jié)點(diǎn)
有點(diǎn)理解不了,可能我childs[0].nodeType!=1沒有理解好吧
2018-08-26
上面while() 執(zhí)行后?conCN[0].nodeType != 1 這句就成==1了。所以if()就不成立導(dǎo)致if括號(hào)里面newDiv.innerHTML = "clear \"" + rmNode.innerHTML + "\" done!";(其實(shí)這句話就是賦值null的意思? ?如果要完全刪除對(duì)象,給 x 賦 null?值,)