在下面的脚本中,我试图通过DOM树,但我没有得到我期望的输出。为什么我在通过DOM树时没有从文本节点获取值?
只有在此一小块HTML的:
<p id="para">This is inside the <em>p</em> tag.</p>
当我在树的这个举动是什么,我得到:
Node Name : P
Node Type : 1
Node Value : null
Node Name : HTML
Node Type : 1
Node Value : null
<html>
<head>
<title>JavaScript</title>
</head>
<body>
<p id="para">This is inside the <em>p</em> tag.</p>
<script type="text/javascript">
function nodeStatus(node) {
document.write("Node Name : " + node.nodeName + "<br />");
document.write("Node Type : " + node.nodeType + "<br/>");
document.write("Node Value : " + node.nodeValue + "<br/>");
document.write("<br/> <br/>");
}
var curElement = document.getElementById("para");
nodeStatus(curElement); // p tag
curElement = document.firstChild; // This is inside the
nodeStatus(curElement);
curElement = document.nextSibling; // em tag
nodeStatus(curElement);
curElement = document.firstChild; // p
nodeStatus(curElement);
</script>
</body>
W我不知道从text-node
得到的价值吗?
什么是HTML我得到的节点名称?我没有将任何节点命名为HTML。
的jsfiddle:http://jsfiddle.net/HmkJQ/
当然你有''节点。看看你的文档的顶部! – Bergi 2013-02-27 09:47:18