我運行這個Javascript代碼,並獲取body.childNode沒有問題的nodeType和nodeName。但是,nodeValue無法顯示。body.childNodes的nodeValue在哪裏?
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get the node types of the body element's child nodes.</p>
<button onclick="myFunction()">Try it</button>
<p><strong>Note:</strong> Whitespace inside elements is considered as text, and text is considered as nodes.</p>
<!-- My personal comment goes here.. -->
<div><strong>Note:</strong> Comments in the document are considered as comment nodes.</div>
<p id="demo"></p>
<script>
function myFunction() {
var c = document.body.childNodes;
var txt = "";
var i;
for (i = 0; i < c.length; i++) {
txt = txt + "notdType: "+ c[i].nodeType + " NodeName: "+c[i].nodeName+" NodeValue: "+c[i].nodeValue +"<br>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
想一想:爲什麼代碼遍歷document.body.childNodes而不是找到它的nodeValue?難道是因爲document.body沒有nodeValue嗎?然後嘗試將沒有nodeValue的主體與沒有nodeValue的節點關聯起來。然後,看看你是否可以找出爲什麼這些其他節點沒有nodeValue。 – BoltClock