流浪的白小菜
2019-11-07 09:44
<!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 属性是属性节点
注释是注释节点

元素节点的 nodeValue 是 undefined 或 null
边上的说明解释的这么清楚了。。。。 这三个都是元素节点
参考9-7【节点属性】这一章的说明,元素节点的nodeValue是undefined或null

html,title,li,button都是元素节点,所以nodeValue为null
想获取一个不存在的元素节点(比如<span>节点),返回的值就为undefined
JavaScript进阶篇
469062 学习 · 22582 问题
相似问题