為什么返回的nodeValue全是null
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>節點屬性</title> </head> <body> ??<ul> ?????<li>javascript</li> ?????<li>HTML/CSS</li> ?????<li>jQuery</li>????? ??</ul> ??<input?type="text"?value="試試看"> ???<input?type="button"?value="點我試試看"> ????<input?type="text"?value="無厘頭"> ????<button>點我</button> ??<script?type="text/javascript"> ????var?con=document.getElementsByTagName("li"); ????for(i=0;i<con.length;i++) ????{ ????????document.write(con[i].nodeName+"?"+con[i].nodeValue+"?"+con[i].nodeType+"<br>"); ????} ????var?icon=document.getElementsByTagName("input"); ????for(i=0;i<con.length;i++) ????{ ????????document.write(icon[i].nodeName+"?"+icon[i].nodeValue+"?"+icon[i].nodeType+"<br>"); ????} ????var?bcon=document.getElementsByTagName("button"); ????document.write(bcon[0].nodeName+"?"+bcon[0].nodeValue+"?"+bcon[0].nodeType+"<br>"); ????var?hcon=document.getElementsByTagName("html"); ????document.write(hcon[0].nodeName+"?"+hcon[0].nodeValue+"?"+hcon[0].nodeType+"<br>"); ????var?tcon=document.getElementsByTagName("title"); ????document.write(tcon[0].nodeName+"?"+tcon[0].nodeValue+"?"+tcon[0].nodeType+"<br>"); ??</script> </body> </html>
結果:
根據 W3C 的 HTML DOM 標準,HTML 文檔中的所有內容都是節點:
整個文檔是一個文檔節點
每個 ? ? HTML 元素是元素節點
HTML? ? ?元素內的文本是文本節點
每個 ? ? HTML 屬性是屬性節點
注釋是注釋節點
2020-04-05
元素節點的 nodeValue 是?undefined 或 null
邊上的說明解釋的這么清楚了。。。。? 這三個都是元素節點
2019-11-08
參考9-7【節點屬性】這一章的說明,元素節點的nodeValue是undefined或null
html,title,li,button都是元素節點,所以nodeValue為null
想獲取一個不存在的元素節點(比如<span>節點),返回的值就為undefined