用chiliNodes最后一行不能輸出
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
</head>
<body>
<div id="con">
? <p>javascript</p>
? <div>jQuery</div>
? <h5>PHP</h5>
</div>
<script type="text/javascript">
? var x=document.getElementById("con");
?document.write("div的第一個子節點是:"+x.firstChild.nodeType+"<br>");
?document.write("div的最后一個子節點是:"+x.lastChild.nodeType+"<br>");
?document.write("div的最后一個子節點是:"+x.childNodes[x.length-1].nodeType);
</script>
</body>
</html>
無法輸出,瀏覽器編譯顯示nodeType未定義
2020-04-28
不應該是x.length 而是?x.childNodes.length
2019-09-24
x.childNodes[x.length-1].nodeType這里的x.length-1是錯誤的,因為這里x是元素節點,你直接在后面添加length屬性所得到的是undefined,所以才不能輸出。
正確的是: