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