我正在練習HTML DOM,我想弄清楚爲什麼第一個腳本工作並顯示childNodes;但第二個腳本沒有。JavaSCRIPT中的空白錯誤
<html><head><title>The Nodes</title>
<style>
p{font-size: x-large; color:darkblue;font-style:bold;
}
</style>
</head>
<body>
<h1>Walking with Nodes</h1>
<p>Who knows what node?</p>
<p>
<script type="text/javascript">
var Parent=document.childNodes[0]; // First childnode is HTML
var Child=Parent.childNodes[0];// Parent’s first child is HEAD
document.write("The parent node is: ");
document.write(Parent.nodeName+"<br />"); //Get the name parent node
document.write("The first child of the parent node is: ");
document.write(Child.nodeName+"<br />");
document.write("The node below the child is: ");
document.write(Child.childNodes[0].nodeName+"<br />");
document.write("The text node below title is: ");
document.write(Child.childNodes[0].childNodes[0].nodeName +"<br />");
document.write("The value of the text node is: ")
document.write(Child.childNodes[0].childNodes[0].nodeValue+"<br/>");
document.write("The first child of the parent is: ");
document.write(Parent.firstChild.nodeName+"<br />");
document.write("The last child of the parent is: ");
document.write(Parent.lastChild.nodeName+"<br />");
document.write("The node below the body is: ");
document.write(Parent.lastChild.childNodes[0].nodeName+"<br />");
document.write("The next sibling of the h1 element is: ");
document.write(Parent.lastChild.childNodes[0].nextSibling.nodeName+"<br/>");
document.write("It's value is " +
Parent.lastChild.childNodes[0].nextSibling.nodeValue);
document.write("<br>The last child's type is: ");
document.write(Parent.lastChild.nodeType);
</script>
</p>
</body>
</html>
現在上面的代碼運行正常;但下面的一個不,我試着通過螢火蟲調試它;但我什麼也沒得到。
<!DOCTYPE html>
<html lang="en-US">
<head><title>the Nodes</title>
<style type="text/css">
p {
font-size: x-large;color:darkblue;font-style:bold;
}
</style>
<meta charset="utf-8">
</head>
<body>
<h1>Practicing with nodes</h1>
<p>Who knows what node?</p>
<p>
<script type="text/javascript">
//<![CDATA[
var Parent = document.childNodes[0]; // documents child is HTML
var Child = Parent.childNodes[0]; // first child node of HTML is head
document.write("The parent node is: ");
document.write(Parent.nodeName+"<br>");
document.write("The first child of the parent node is: ");
document.write(Child.nodeName+"<br>");
document.write("The parent node is: ");
document.write(Parent.nodeName+"<br />"); //Get the name parent node
document.write("The first child of the parent node is: ");
document.write(Child.nodeName+"<br />");
document.write("The node below the child is: ");
document.write(Child.childNodes[0].nodeName+"<br />");
document.write("The text node below title is: ");
document.write(Child.childNodes[0].childNodes[0].nodeName +"<br />");
document.write("The value of the text node is: ")
document.write(Child.childNodes[0].childNodes[0].nodeValue+"<br/>");
document.write("The first child of the parent is: ");
document.write(Parent.firstChild.nodeName+"<br />");
document.write("The last child of the parent is: ");
document.write(Parent.lastChild.nodeName+"<br />");
document.write("The node below the body is: ");
document.write(Parent.lastChild.childNodes[0].nodeName+"<br />");
document.write("The next sibling of the h1 element is: ");
document.write(Parent.lastChild.childNodes[0].nextSibling.nodeName+"<br/>");
document.write("It's value is " +
Parent.lastChild.childNodes[0].nextSibling.nodeValue);
document.write("<br>The last child's type is: ");
document.write(Parent.lastChild.nodeType);
//]]>
</script>
</p>
</body>
</html>
有人可以請我解釋我做錯了什麼,以及什麼將空白的bug與上面的腳本有關。謝謝大家:)
'Parent.childNodes [0]'未定義。換句話說,'Parent.childNodes.length'等於「0」 –
你爲什麼使用doucument.write()? – epascarello
您可以強調頂部和底部代碼之間的區別嗎?我看不出有什麼不同。我所看到的是大量的'document.write()' – zero298